Skip to content

Commit 6323491

Browse files
add more solutions ⚡
1 parent f83c36f commit 6323491

File tree

58 files changed

+1304
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1304
-67
lines changed

design pattern/file_search.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ class Type: public Filter {
5454

5555
class FindFiles {
5656
public:
57-
void searchFilesDFS(File f, vector < Filter > filters, vector < File * > & output) {
58-
if (f -> children.empty()) {
57+
void searchFilesDFS(File *f, vector < Filter > filters, vector < File * > & output) {
58+
if (f->children.empty()) {
5959
return;
6060
}
6161

62-
for (auto file: f -> children) {
62+
for (auto file: f->children) {
6363
bool flag = true;
6464
if (file -> isDirectory) {
6565
searchFilesDFS(file, filters, output);
6666
} else {
6767
for (auto it: filters) {
68-
if (it -> apply(file) == false) {
68+
if (it.apply(file) == false) {
6969
flag = false;
7070
break;
7171
}

design pattern/linux_file_search.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#include <bits/stdc++.h>
22
using namespace std;
33

4+
// Unix File Search API
5+
6+
// Design Unix File Search API to search file with different arguments as "extension", "name", "size" ...
7+
// The design should be maintainable to add new contraints.
8+
49
enum FileType {
5-
DIR = 0,
6-
XML,
7-
TXT,
8-
COUNT
10+
DIR = 0, XML, TXT, COUNT
911
};
1012

1113
class File {
@@ -131,7 +133,8 @@ class TypeSameRule : public Rule {
131133
private:
132134
FileType target_type_ = FileType::COUNT;
133135
public:
134-
TypeSameRule(FileType type) : target_type_(type){}
136+
TypeSameRule(FileType type) : target_type_(type){ }
137+
135138
bool checkRulePassed(File* file) const override{
136139
return file->getType() == target_type_;
137140
}
@@ -143,7 +146,6 @@ class TypeSameRule : public Rule {
143146

144147

145148
int main() {
146-
147149
Directory cur_dir{"test_dir"};
148150
for(size_t i = 0; i < 10; i++) {
149151
File* cur_file = new File("test_file" + to_string(i), i * 100, FileType(i % FileType::COUNT));
@@ -161,7 +163,6 @@ int main() {
161163

162164
vector<File*> flitering_res = cur_filter.find_filter(cur_dir);
163165
cout << "Filtering results:\n";
164-
for(auto f : flitering_res)
165-
f->printInfo();
166+
for(auto f : flitering_res) f->printInfo();
166167
return 0;
167168
}
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* struct ListNode {
4-
* int val;
5-
* ListNode *next;
6-
* ListNode() : val(0), next(nullptr) {}
7-
* ListNode(int x) : val(x), next(nullptr) {}
8-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9-
* };
10-
*/
111
class Solution {
122
public:
133
ListNode* swapPairs(ListNode* head) {
14-
auto itr = new ListNode(-1, head);
15-
auto prev = itr;
16-
while (head && head->next) {
17-
auto first = head, second = head->next;
4+
if (!head) return NULL;
5+
ListNode *itr = head, *prev = NULL, *ret = head->next;
6+
if (!ret) return head;
7+
8+
while (itr && itr->next) {
9+
auto l = itr, r = l->next, cache = r->next;
10+
r->next = l;
11+
l->next = cache;
12+
itr = cache;
1813

19-
prev->next = second;
20-
first->next = second->next;
21-
second->next = first;
22-
23-
prev = first;
24-
head = first->next;
14+
if (prev) prev->next = r;
15+
prev = l;
2516
}
26-
return itr->next;
17+
return ret;
2718
}
2819
};

leetcode/0053-maximum-subarray/0053-maximum-subarray.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class Solution {
22
public:
3-
int maxSubArray(vector<int>& nums, int res = INT_MIN, int local = 0) {
4-
for (auto num: nums) {
5-
local = num + max(local, 0);
6-
res = max(local, res);
3+
int maxSubArray(vector<int>& nums) {
4+
int add = nums[0], res = nums[0];
5+
for (int i = 1; i < nums.size(); i++) {
6+
add = nums[i] + max(add, 0),
7+
res = max(res, add);
78
}
89
return res;
910
}
Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
1-
// class Solution {
2-
// public:
3-
// vector<int> topKFrequent(vector<int>& nums, int k) {
4-
// unordered_map<int, int> counter;
5-
// for (auto num: nums) counter[num]++;
6-
// priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
7-
// for (auto [key, frequency]: counter) {
8-
// pq.push({frequency, key});
9-
// if (pq.size() > k) pq.pop();
10-
// }
11-
// vector<int> res;
12-
// while(pq.size()) {
13-
// auto elem = pq.top(); pq.pop();
14-
// res.push_back(elem.second);
15-
// }
16-
// return res;
17-
// }
18-
// };
19-
20-
211
class Solution {
222
public:
233
vector<int> topKFrequent(vector<int>& nums, int k) {
24-
unordered_map<int, int> counter;
25-
for (auto num: nums) counter[num]++;
26-
vector<vector<int>> freqMapper(nums.size() + 1);
27-
for (auto [key, freq]: counter) freqMapper[freq].push_back(key);
28-
reverse(begin(freqMapper), end(freqMapper));
29-
vector<int> res;
30-
for (auto elem: freqMapper)
31-
for (auto i: elem) {
32-
res.push_back(i);
33-
if (res.size() == k) return res;
4+
5+
priority_queue<pair<int, int>,
6+
vector<pair<int, int>>,
7+
greater<pair<int, int>>> pq;
8+
9+
sort(nums.begin(), nums.end());
10+
nums.push_back(INT_MAX);
11+
12+
int prev = INT_MIN, count = 1;
13+
for (auto num: nums) {
14+
if ( num != prev && prev != INT_MIN ) {
15+
pq.push({count, prev});
16+
if (pq.size() > k) pq.pop();
3417
}
18+
count = num == prev ? count + 1: 1;
19+
prev = num;
20+
}
21+
22+
vector<int> res;
23+
while (pq.size())
24+
res.push_back(pq.top().second),
25+
pq.pop();
3526
return res;
3627
}
3728
};
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
class UF {
2+
public:
3+
string find(string s) {
4+
auto is_parent_available = parents.count(s);
5+
if (is_parent_available) {
6+
auto p = parents[s], gp = find(p);
7+
vals[s] *= vals[p];
8+
parents[s] = gp;
9+
}
10+
return parents[s];
11+
}
12+
13+
void join(string p, string c, double val) {
14+
add(p), add(c);
15+
auto px = find(p), py = find(c);
16+
parents[px] = py;
17+
vals[px] = val * vals[p] / vals[c];
18+
}
19+
20+
bool has(string s, string c) {
21+
if (!parents.count(s)) return false;
22+
if (!parents.count(c)) return false;
23+
if (find(s) == find(c)) return false;
24+
return true;
25+
}
26+
27+
double get_ans(string p, string c) {
28+
return vals[p] / vals[c];
29+
}
30+
private:
31+
unordered_map<string, double> vals;
32+
unordered_map<string, string> parents;
33+
34+
void add(string s) {
35+
if (parents.count(s)) return;
36+
parents[s] = s;
37+
vals[s] = 1.0;
38+
}
39+
};
40+
41+
class Solution {
42+
// public:
43+
// vector<double> calcEquation(vector<vector<string>>& eqs,
44+
// vector<double>& vals,
45+
// vector<vector<string>>& qs) {
46+
// UF uf;
47+
// for (int i = 0; i < vals.size(); i++) {
48+
// auto p = eqs[i][0], c = eqs[i][1];
49+
// auto weight = vals[i];
50+
// uf.join(p, c, weight);
51+
// }
52+
// vector<double> res;
53+
// for (auto q: qs) {
54+
// auto calc = uf.has(q[0], q[1]) ? uf.get_ans(q[0], q[1]): -1;
55+
// res.push_back(calc);
56+
// }
57+
// return res;
58+
// }
59+
public:
60+
vector<double> calcEquation(vector<vector<string>>& equations,
61+
vector<double>& values,
62+
vector<vector<string>>& queries) {
63+
64+
vector<double> res(queries.size(), -1);
65+
66+
int i = 0;
67+
for (auto &eq : equations)
68+
unite(eq[0], eq[1], values[i++]);
69+
70+
i = 0;
71+
for (auto &q : queries) {
72+
string x = q[0], y = q[1];
73+
if (isNode(x) && isNode(y) && find(x) == find(y))
74+
res[i] = vals[x] / vals[y];
75+
++i;
76+
}
77+
78+
return res;
79+
}
80+
81+
bool isNode(string x) {
82+
return roots.count(x) > 0;
83+
}
84+
85+
void add(string x) {
86+
if (!isNode(x)) roots[x] = x, vals[x] = 1.0;
87+
}
88+
89+
string find(string x) {
90+
if (x != roots[x]) {
91+
string rootX = roots[x];
92+
roots[x] = find(rootX);
93+
vals[x] = vals[x] * vals[rootX];
94+
}
95+
return isNode(x) ? roots[x] : x;
96+
}
97+
98+
void unite(string x, string y, double v) {
99+
add(x), add(y);
100+
string rootX = find(x), rootY = find(y);
101+
roots[rootX] = rootY;
102+
vals[rootX] = v * vals[y] / vals[x];
103+
}
104+
105+
private:
106+
unordered_map<string, string> roots;
107+
unordered_map<string, double> vals;
108+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<h2><a href="https://leetcode.com/problems/evaluate-division/">399. Evaluate Division</a></h2><h3>Medium</h3><hr><div><p>You are given an array of variable pairs <code>equations</code> and an array of real numbers <code>values</code>, where <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> and <code>values[i]</code> represent the equation <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code>. Each <code>A<sub>i</sub></code> or <code>B<sub>i</sub></code> is a string that represents a single variable.</p>
2+
3+
<p>You are also given some <code>queries</code>, where <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> represents the <code>j<sup>th</sup></code> query where you must find the answer for <code>C<sub>j</sub> / D<sub>j</sub> = ?</code>.</p>
4+
5+
<p>Return <em>the answers to all queries</em>. If a single answer cannot be determined, return <code>-1.0</code>.</p>
6+
7+
<p><strong>Note:</strong> The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
13+
<strong>Output:</strong> [6.00000,0.50000,-1.00000,1.00000,-1.00000]
14+
<strong>Explanation:</strong>
15+
Given: <em>a / b = 2.0</em>, <em>b / c = 3.0</em>
16+
queries are: <em>a / c = ?</em>, <em>b / a = ?</em>, <em>a / e = ?</em>, <em>a / a = ?</em>, <em>x / x = ?</em>
17+
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre><strong>Input:</strong> equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
23+
<strong>Output:</strong> [3.75000,0.40000,5.00000,0.20000]
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
<pre><strong>Input:</strong> equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
29+
<strong>Output:</strong> [0.50000,2.00000,-1.00000,-1.00000]
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= equations.length &lt;= 20</code></li>
37+
<li><code>equations[i].length == 2</code></li>
38+
<li><code>1 &lt;= A<sub>i</sub>.length, B<sub>i</sub>.length &lt;= 5</code></li>
39+
<li><code>values.length == equations.length</code></li>
40+
<li><code>0.0 &lt; values[i] &lt;= 20.0</code></li>
41+
<li><code>1 &lt;= queries.length &lt;= 20</code></li>
42+
<li><code>queries[i].length == 2</code></li>
43+
<li><code>1 &lt;= C<sub>j</sub>.length, D<sub>j</sub>.length &lt;= 5</code></li>
44+
<li><code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> consist of lower case English letters and digits.</li>
45+
</ul>
46+
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool isBipartite(vector<vector<int>>& graph) {
4+
int n = graph.size();
5+
vector<int> colors(n, 0);
6+
queue<int> q;
7+
for (int i = 0; i < n; i++) {
8+
if (colors[i]) continue;
9+
q.push(i); colors[i] = 1;
10+
while (q.size()) {
11+
auto top = q.front(); q.pop();
12+
for (auto to: graph[top])
13+
if (colors[to] == 0)
14+
colors[to] = -colors[top], q.push(to);
15+
else
16+
if (colors[to] == colors[top]) return false;
17+
}
18+
}
19+
return true;
20+
}
21+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)