Skip to content

Commit 2c0e8ef

Browse files
committed
12 hard problems
1 parent eb4d75c commit 2c0e8ef

13 files changed

+888
-0
lines changed

Diff for: alien-dictionary_1_AC.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Topological sort
2+
// Number of nodes is too small, so don't bother with heap optimization.
3+
#include <string>
4+
#include <unordered_map>
5+
#include <unordered_set>
6+
#include <utility>
7+
#include <vector>
8+
using std::make_pair;
9+
using std::pair;
10+
using std::string;
11+
using std::unordered_map;
12+
using std::unordered_set;
13+
using std::vector;
14+
15+
class Solution {
16+
public:
17+
string alienOrder(vector<string>& words) {
18+
auto &w = words;
19+
int n = w.size();
20+
21+
unordered_map<char, unordered_set<char>> g;
22+
unordered_map<char, int> ind;
23+
24+
for (auto &s: w) {
25+
for (auto &ch: s) {
26+
g[ch];
27+
ind[ch] = 0;
28+
}
29+
}
30+
int m = ind.size();
31+
32+
int ls, lt;
33+
int i, j;
34+
for (i = 0; i + 1 < n; ++i) {
35+
string &s = w[i];
36+
string &t = w[i + 1];
37+
ls = s.size();
38+
lt = t.size();
39+
for (j = 0; j < ls && j < lt; ++j) {
40+
if (s[j] == t[j]) {
41+
continue;
42+
}
43+
if (g[s[j]].find(t[j]) == g[s[j]].end()) {
44+
g[s[j]].insert(t[j]);
45+
++ind[t[j]];
46+
}
47+
break;
48+
}
49+
}
50+
51+
char ch;
52+
string res = "";
53+
while (true) {
54+
ch = '\0';
55+
for (auto &p: ind) {
56+
if (p.second == 0) {
57+
ch = p.first;
58+
break;
59+
}
60+
}
61+
if (ch == '\0') {
62+
break;
63+
}
64+
for (auto &ch1: g[ch]) {
65+
--ind[ch1];
66+
}
67+
g.erase(ch);
68+
ind.erase(ch);
69+
70+
res.push_back(ch);
71+
--m;
72+
}
73+
if (m > 0) {
74+
res = "";
75+
}
76+
g.clear();
77+
ind.clear();
78+
79+
return res;
80+
}
81+
};

Diff for: best-meeting-point_1_AC.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Make sure you calculate the cumulated sum in the right way.
2+
#include <algorithm>
3+
#include <climits>
4+
#include <vector>
5+
using std::min;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
int minTotalDistance(vector<vector<int>>& grid) {
11+
auto &a = grid;
12+
int n = a.size();
13+
int m = (n > 0 ? a[0].size() : 0);
14+
if (n == 0 || m == 0) {
15+
return 0;
16+
}
17+
18+
vector<int> ci(n, 0), cj(m, 0);
19+
int i, j;
20+
for (i = 0; i < n; ++i) {
21+
for (j = 0; j < m; ++j) {
22+
if (a[i][j] == 1) {
23+
++ci[i];
24+
++cj[j];
25+
}
26+
}
27+
}
28+
29+
vector<int> vl(m, 0), vr(m, 0), vu(n, 0), vd(n, 0);
30+
int sum;
31+
32+
sum = cj[0];
33+
for (j = 1; j <= m - 1; ++j) {
34+
vl[j] = vl[j - 1] + sum;
35+
sum += cj[j];
36+
}
37+
sum = cj[m - 1];
38+
for (j = m - 2; j >= 0; --j) {
39+
vr[j] = vr[j + 1] + sum;
40+
sum += cj[j];
41+
}
42+
sum = ci[0];
43+
for (i = 1; i <= n - 1; ++i) {
44+
vu[i] = vu[i - 1] + sum;
45+
sum += ci[i];
46+
}
47+
sum = ci[n - 1];
48+
for (i = n - 2; i >= 0; --i) {
49+
vd[i] = vd[i + 1] + sum;
50+
sum += ci[i];
51+
}
52+
53+
int res = INT_MAX;
54+
for (i = 0; i < n; ++i) {
55+
for (j = 0; j < m; ++j) {
56+
sum = vl[j] + vr[j] + vu[i] + vd[i];
57+
res = min(res, sum);
58+
}
59+
}
60+
61+
ci.clear();
62+
cj.clear();
63+
64+
vl.clear();
65+
vr.clear();
66+
vu.clear();
67+
vd.clear();
68+
69+
return res;
70+
}
71+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <algorithm>
2+
#include <string>
3+
#include <vector>
4+
using std::max;
5+
using std::string;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
int lengthOfLongestSubstringKDistinct(string s, int k) {
11+
if (k == 0) {
12+
return 0;
13+
}
14+
int ls = s.size();
15+
if (ls <= k) {
16+
return ls;
17+
}
18+
19+
static const int DICT_SIZE = 256;
20+
vector<int> c(DICT_SIZE, 0);
21+
int i, j;
22+
int cc = 0;
23+
int res = 0;
24+
25+
j = 0;
26+
for (i = 0; i < ls; ++i) {
27+
if (c[s[i]] == 0) {
28+
++cc;
29+
}
30+
++c[s[i]];
31+
if (cc <= k) {
32+
res = max(res, i - j + 1);
33+
}
34+
while (cc > k) {
35+
if (c[s[j]] == 1) {
36+
--cc;
37+
}
38+
--c[s[j++]];
39+
}
40+
}
41+
c.clear();
42+
43+
return res;
44+
}
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <algorithm>
2+
#include <string>
3+
#include <vector>
4+
using std::max;
5+
using std::string;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
int lengthOfLongestSubstringTwoDistinct(string s) {
11+
const int k = 2;
12+
int ls = s.size();
13+
if (ls <= k) {
14+
return ls;
15+
}
16+
17+
static const int DICT_SIZE = 256;
18+
vector<int> c(DICT_SIZE, 0);
19+
int i, j;
20+
int cc = 0;
21+
int res = 0;
22+
23+
j = 0;
24+
for (i = 0; i < ls; ++i) {
25+
if (c[s[i]] == 0) {
26+
++cc;
27+
}
28+
++c[s[i]];
29+
if (cc <= k) {
30+
res = max(res, i - j + 1);
31+
}
32+
while (cc > k) {
33+
if (c[s[j]] == 1) {
34+
--cc;
35+
}
36+
--c[s[j++]];
37+
}
38+
}
39+
c.clear();
40+
41+
return res;
42+
}
43+
};

Diff for: minimum-unique-word-abbreviation_1_AC.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// I tried to write an iterative version, but it didn't work.
2+
// Had to make do with DFS after all.
3+
// The idea of bit masking is clever.
4+
#include <string>
5+
#include <unordered_set>
6+
#include <vector>
7+
using std::string;
8+
using std::to_string;
9+
using std::unordered_set;
10+
using std::vector;
11+
12+
class Solution {
13+
public:
14+
string minAbbreviation(string target, vector<string>& dictionary) {
15+
string &s = target;
16+
int ls = s.size();
17+
int max_bt = (1 << ls);
18+
19+
unordered_set<int> us;
20+
int bt;
21+
int lt;
22+
int i;
23+
for (string &t: dictionary) {
24+
bt = 0;
25+
lt = t.size();
26+
if (ls != lt) {
27+
continue;
28+
}
29+
bt = 0;
30+
for (i = 0; i < ls; ++i) {
31+
if (s[i] == t[i]) {
32+
bt |= (1 << i);
33+
}
34+
}
35+
markBit(us, bt);
36+
}
37+
if (us.size() == 0) {
38+
return to_string(ls);
39+
}
40+
41+
string res = s;
42+
dfs(1, 0, 0, 0, s, us, res);
43+
dfs(2, 0, 0, 0, s, us, res);
44+
us.clear();
45+
46+
return res;
47+
}
48+
private:
49+
void dfs(int type, int idx, int len, int bt, string &s,
50+
unordered_set<int> &us, string &res) {
51+
// Pruning here
52+
if (len >= res.size()) {
53+
return;
54+
}
55+
56+
int ls = s.size();
57+
if (idx == ls) {
58+
if (us.find(bt) == us.end()) {
59+
res = bitToString(s, bt);
60+
}
61+
return;
62+
}
63+
64+
int next_len;
65+
int next_bt;
66+
int i, j;
67+
for (i = ls; i > idx; --i) {
68+
next_bt = bt;
69+
if (type == 1) {
70+
next_len = len + 1;
71+
} else {
72+
next_len = len + i - idx;
73+
for (j = idx; j < i; ++j) {
74+
next_bt |= (1 << j);
75+
}
76+
}
77+
dfs(3 - type, i, next_len, next_bt, s, us, res);
78+
}
79+
}
80+
81+
void markBit(unordered_set<int> &us, int bt) {
82+
if (us.find(bt) != us.end()) {
83+
return;
84+
}
85+
us.insert(bt);
86+
87+
int i = 1;
88+
while (i <= bt) {
89+
if (bt & i) {
90+
markBit(us, bt ^ i);
91+
}
92+
i <<= 1;
93+
}
94+
}
95+
96+
string bitToString(string &s, int bt) {
97+
string res = "";
98+
int last_i = -1;
99+
int i;
100+
for (i = 0; (1 << i) <= bt; ++i) {
101+
if ((bt & (1 << i)) == 0) {
102+
continue;
103+
}
104+
if (i - 1 > last_i) {
105+
res += to_string(i - 1 - last_i);
106+
}
107+
res.push_back(s[i]);
108+
last_i = i;
109+
}
110+
111+
int ls = s.size();
112+
if (ls - 1 > last_i) {
113+
res += to_string(ls - 1 - last_i);
114+
}
115+
116+
return res;
117+
}
118+
};

0 commit comments

Comments
 (0)