Skip to content

Commit 5b37a6a

Browse files
committed
418/421
1 parent 4e9a79a commit 5b37a6a

13 files changed

+308
-1
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 406/421 problems.
5+
Solved 418/421 problems.
66

77
## Database
88

@@ -13,11 +13,21 @@ See [database.md](database.md)
1313
| # | Title | Solution |
1414
|---| ----- | -------- |
1515
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements)|[minimum-moves-to-equal-array-elements.cc](minimum-moves-to-equal-array-elements.cc)|
16+
|452|[Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons)|[minimum-number-of-arrows-to-burst-balloons.cc](minimum-number-of-arrows-to-burst-balloons.cc)|
1617
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs)|[number-of-boomerangs.cc](number-of-boomerangs.cc)|
18+
|446|[Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence)|[arithmetic-slices-ii-subsequence.cc](arithmetic-slices-ii-subsequence.cc)|
19+
|444|[Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction)|[sequence-reconstruction.cc](sequence-reconstruction.cc)|
20+
|441|[Arranging Coins](https://leetcode.com/problems/arranging-coins)|[arranging-coins.cc](arranging-coins.cc)|
21+
|440|[K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order)|[k-th-smallest-in-lexicographical-order.cc](k-th-smallest-in-lexicographical-order.cc)|
22+
|439|[Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser)|[ternary-expression-parser.cc](ternary-expression-parser.cc)|
1723
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string)|[find-all-anagrams-in-a-string.cc](find-all-anagrams-in-a-string.cc)|
1824
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii)|[path-sum-iii.cc](path-sum-iii.cc)|
1925
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval)|[find-right-interval.cc](find-right-interval.cc)|
2026
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals)|[non-overlapping-intervals.cc](non-overlapping-intervals.cc)|
27+
|432|[All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure)|[all-oone-data-structure.cc](all-oone-data-structure.cc)|
28+
|424|[Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement)|[longest-repeating-character-replacement.cc](longest-repeating-character-replacement.cc)|
29+
|423|[Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english)|[reconstruct-original-digits-from-english.cc](reconstruct-original-digits-from-english.cc)|
30+
|422|[Valid Word Square](https://leetcode.com/problems/valid-word-square)|[valid-word-square.cc](valid-word-square.cc)|
2131
|421|[Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array)|[maximum-xor-of-two-numbers-in-an-array.cc](maximum-xor-of-two-numbers-in-an-array.cc)|
2232
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker)|[strong-password-checker.cc](strong-password-checker.cc)|
2333
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board)|[battleships-in-a-board.cc](battleships-in-a-board.cc)|
@@ -29,6 +39,8 @@ See [database.md](database.md)
2939
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz)|[fizz-buzz.cc](fizz-buzz.cc)|
3040
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum)|[split-array-largest-sum.cc](split-array-largest-sum.cc)|
3141
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome)|[longest-palindrome.cc](longest-palindrome.cc)|
42+
|408|[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation)|[valid-word-abbreviation.cc](valid-word-abbreviation.cc)|
43+
|407|[Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii)|[trapping-rain-water-ii.cc](trapping-rain-water-ii.cc)|
3244
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height)|[queue-reconstruction-by-height.cc](queue-reconstruction-by-height.cc)|
3345
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal)|[convert-a-number-to-hexadecimal.cc](convert-a-number-to-hexadecimal.cc)|
3446
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves)|[sum-of-left-leaves.cc](sum-of-left-leaves.cc)|

all-oone-data-structure.cc

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// All O`one Data Structure
2+
class AllOne {
3+
struct Bucket { int v; unordered_set<string> ks; };
4+
unordered_map<string, list<Bucket>::iterator> a;
5+
list<Bucket> b;
6+
public:
7+
/** Initialize your data structure here. */
8+
AllOne() {
9+
b.push_front({0, {}});
10+
}
11+
12+
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
13+
void inc(string key) {
14+
if (! a.count(key)) {
15+
a[key] = b.begin();
16+
b.begin()->ks.insert(key);
17+
}
18+
auto y = a[key], x = y++;
19+
if (y == b.end() || x->v+1 < y->v)
20+
y = b.insert(y, {x->v+1, {}});
21+
a[key] = y;
22+
y->ks.insert(key);
23+
x->ks.erase(key);
24+
if (x->ks.empty() && x->v)
25+
b.erase(x);
26+
}
27+
28+
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
29+
void dec(string key) {
30+
if (a.count(key)) {
31+
auto y = a[key], x = y--;
32+
if (x->v > 1) {
33+
if (x == b.begin() || y->v < x->v-1)
34+
y = b.insert(x, {x->v-1, {}});
35+
a[key] = y;
36+
y->ks.insert(key);
37+
} else
38+
a.erase(key);
39+
x->ks.erase(key);
40+
if (x->ks.empty())
41+
b.erase(x);
42+
}
43+
}
44+
45+
/** Returns one of the keys with maximal value. */
46+
string getMaxKey() {
47+
return b.size() == 1 ? "" : *b.rbegin()->ks.begin();
48+
}
49+
50+
/** Returns one of the keys with Minimal value. */
51+
string getMinKey() {
52+
return b.size() == 1 ? "" : *(++b.begin())->ks.begin();
53+
}
54+
};
55+
56+
/**
57+
* Your AllOne object will be instantiated and called as such:
58+
* AllOne obj = new AllOne();
59+
* obj.inc(key);
60+
* obj.dec(key);
61+
* string param_3 = obj.getMaxKey();
62+
* string param_4 = obj.getMinKey();
63+
*/

arithmetic-slices-ii-subsequence.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Arithmetic Slices II - Subsequence
2+
class Solution {
3+
public:
4+
int numberOfArithmeticSlices(vector<int>& A) {
5+
int r = 0;
6+
vector<unordered_map<long, int>> s(A.size());
7+
for (int i = 1; i < A.size(); i++)
8+
for (int j = 0; j < i; j++) {
9+
long d = long(A[i])-A[j];
10+
if (s[j].count(d)) {
11+
r += s[j][d];
12+
s[i][d] += s[j][d];
13+
}
14+
s[i][d]++;
15+
}
16+
return r;
17+
}
18+
};

arranging-coins.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Arranging Coins
2+
class Solution {
3+
public:
4+
int arrangeCoins(int n) {
5+
return sqrt(2.*n+0.25)-0.5;
6+
}
7+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// K-th Smallest in Lexicographical Order
2+
class Solution {
3+
public:
4+
int findKthNumber(int n, int k) {
5+
long r = 1;
6+
while (k > 1) {
7+
long c = 0, x = r, y = r+1;
8+
for (; x <= n; x *= 10, y *= 10)
9+
c += min(n+1L, y)-x;
10+
if (c < k)
11+
k -= c, r++;
12+
else
13+
k--, r *= 10;
14+
}
15+
return r;
16+
}
17+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Longest Repeating Character Replacement
2+
class Solution {
3+
public:
4+
int characterReplacement(string s, int k) {
5+
int x = 0, mx = 0, c[127] = {};
6+
for (int i = 0; i < s.size(); i++) {
7+
mx = max(mx, ++c[s[i]]);
8+
if (mx+k <= i-x)
9+
c[s[x++]]--;
10+
}
11+
return s.size()-x;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Minimum Number of Arrows to Burst Balloons
2+
class Solution {
3+
public:
4+
int findMinArrowShots(vector<pair<int, int>>& points) {
5+
int r = 0;
6+
sort(points.begin(), points.end(), [](const pair<int, int>& x, const pair<int, int>& y) {
7+
return x.second < y.second;
8+
});
9+
for (int i = 0; i < points.size(); ) {
10+
r++;
11+
int t = points[i].second;
12+
while (++i < points.size() && points[i].first <= t);
13+
}
14+
return r;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Reconstruct Original Digits from English
2+
class Solution {
3+
public:
4+
string originalDigits(string s) {
5+
int c[127] = {}, a[10];
6+
for (char i: s)
7+
c[i]++;
8+
a[0] = c['z'];
9+
a[2] = c['w'];
10+
a[4] = c['u'];
11+
a[6] = c['x'];
12+
a[8] = c['g'];
13+
a[1] = c['o']-a[0]-a[2]-a[4];
14+
a[3] = c['h']-a[8];
15+
a[5] = c['f']-a[4];
16+
a[7] = c['v']-a[5];
17+
a[9] = c['i']-a[5]-a[6]-a[8];
18+
string r;
19+
for (int i = 0; i < 10; i++)
20+
r += string(a[i], '0'+i);
21+
return r;
22+
}
23+
};

sequence-reconstruction.cc

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Sequence Reconstruction
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
8+
int n = org.size();
9+
vector<int> d(n, 0);
10+
vector<char> v(n, 0);
11+
vector<vector<int>> e(n);
12+
for (auto& seq: seqs) {
13+
for (int i: seq) {
14+
if (i <= 0 || i > n)
15+
return false;
16+
v[i-1] = 1;
17+
}
18+
REP(i, int(seq.size())-1) {
19+
e[seq[i]-1].push_back(seq[i+1]-1);
20+
d[seq[i+1]-1]++;
21+
}
22+
}
23+
if (count(v.begin(), v.end(), 0))
24+
return false;
25+
int r = 0, l = 0, x = -1;
26+
REP(i, n)
27+
if (! d[i]) {
28+
if (x >= 0) return false;
29+
x = i;
30+
}
31+
while (x >= 0) {
32+
if (x != org[l++]-1) return false;
33+
int u = x;
34+
x = -1;
35+
for (int y: e[u])
36+
if (! --d[y]) {
37+
if (x >= 0) return false;
38+
x = y;
39+
}
40+
}
41+
return l == n;
42+
}
43+
};

ternary-expression-parser.cc

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Ternary Expression Parser
2+
class Solution {
3+
string f(string& e, int& i) {
4+
char a = e[i];
5+
i += 2;
6+
if (i-1 == e.size() || e[i-1] != '?')
7+
return string(1, a);
8+
string l = f(e, i), r = f(e, i);
9+
return a == 'T' ? l : r;
10+
}
11+
public:
12+
string parseTernary(string expression) {
13+
int i = 0;
14+
return f(expression, i);
15+
}
16+
};

trapping-rain-water-ii.cc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Trapping Rain Water II
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int trapRainWater(vector<vector<int>>& h) {
8+
if (h.size() < 3 || h[0].size() < 3) return 0;
9+
int r = 0, m = h.size(), n = h[0].size(), x, y;
10+
vector<vector<char>> v(m, vector<char>(n, 0));
11+
vector<vector<pair<int, int>>> s(20001);
12+
REP(i, m) {
13+
s[h[i][0]].emplace_back(i, 0);
14+
s[h[i][n-1]].emplace_back(i, n-1);
15+
v[i][0] = v[i][n-1] = 1;
16+
}
17+
FOR(i, 1, n) {
18+
s[h[0][i]].emplace_back(0, i);
19+
s[h[m-1][i]].emplace_back(m-1, i);
20+
v[0][i] = v[m-1][i] = 1;
21+
}
22+
REP(i, s.size())
23+
while (s[i].size()) {
24+
tie(x, y) = s[i].back();
25+
s[i].pop_back();
26+
REP(d, 4) {
27+
int xx = x+((int[]){-1,0,1,0})[d], yy = y+((int[]){0,1,0,-1})[d];
28+
if (unsigned(xx) < m && unsigned(yy) < n && ! v[xx][yy]) {
29+
v[xx][yy] = 1;
30+
int t = max(h[x][y], h[xx][yy]);
31+
r += t-h[xx][yy];
32+
h[xx][yy] = t;
33+
s[t].emplace_back(xx, yy);
34+
}
35+
}
36+
}
37+
return r;
38+
}
39+
};

valid-word-abbreviation.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Valid Word Abbreviation
2+
class Solution {
3+
public:
4+
bool validWordAbbreviation(string word, string abbr) {
5+
int j = 0;
6+
for (int i = 0; i < abbr.size(); ) {
7+
if (j >= word.size())
8+
return false;
9+
if (isdigit(abbr[i])) {
10+
if (abbr[i] == '0') return false;
11+
int t = abbr[i]-'0';
12+
while (++i < abbr.size() && isdigit(abbr[i])) {
13+
t = t*10+abbr[i]-'0';
14+
if (j+t > word.size()) return false;
15+
}
16+
j += t;
17+
} else if (abbr[i++] != word[j++])
18+
return false;
19+
}
20+
return j == word.size();
21+
}
22+
};

valid-word-square.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Valid Word Square
2+
class Solution {
3+
public:
4+
bool validWordSquare(vector<string>& words) {
5+
int n = words.size();
6+
if (! n) return true;
7+
int m = words[0].size();
8+
if (n != m) return false;
9+
for (int i = 1; i < n; i++)
10+
if (words[i-1].size() < words[i].size())
11+
return false;
12+
for (int i = 0; i < n; i++)
13+
for (int j = 0; j < words[i].size(); j++)
14+
if (words[j].size() <= i || words[i][j] != words[j][i])
15+
return false;
16+
return true;
17+
}
18+
};

0 commit comments

Comments
 (0)