Skip to content

Commit 4e9a79a

Browse files
committed
406/421
1 parent 51d3ea3 commit 4e9a79a

21 files changed

+425
-1
lines changed

Diff for: README.md

+21-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 386/386 problems.
5+
Solved 406/421 problems.
66

77
## Database
88

@@ -12,6 +12,26 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|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+
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs)|[number-of-boomerangs.cc](number-of-boomerangs.cc)|
17+
|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)|
18+
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii)|[path-sum-iii.cc](path-sum-iii.cc)|
19+
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval)|[find-right-interval.cc](find-right-interval.cc)|
20+
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals)|[non-overlapping-intervals.cc](non-overlapping-intervals.cc)|
21+
|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)|
22+
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker)|[strong-password-checker.cc](strong-password-checker.cc)|
23+
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board)|[battleships-in-a-board.cc](battleships-in-a-board.cc)|
24+
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow)|[pacific-atlantic-water-flow.cc](pacific-atlantic-water-flow.cc)|
25+
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum)|[partition-equal-subset-sum.cc](partition-equal-subset-sum.cc)|
26+
|415|[Add Strings](https://leetcode.com/problems/add-strings)|[add-strings.cc](add-strings.cc)|
27+
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number)|[third-maximum-number.cc](third-maximum-number.cc)|
28+
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices)|[arithmetic-slices.cc](arithmetic-slices.cc)|
29+
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz)|[fizz-buzz.cc](fizz-buzz.cc)|
30+
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum)|[split-array-largest-sum.cc](split-array-largest-sum.cc)|
31+
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome)|[longest-palindrome.cc](longest-palindrome.cc)|
32+
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height)|[queue-reconstruction-by-height.cc](queue-reconstruction-by-height.cc)|
33+
|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)|
34+
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves)|[sum-of-left-leaves.cc](sum-of-left-leaves.cc)|
1535
|403|[Frog Jump](https://leetcode.com/problems/frog-jump)|[frog-jump.cc](frog-jump.cc)|
1636
|402|[Remove K Digits](https://leetcode.com/problems/remove-k-digits)|[remove-k-digits.cc](remove-k-digits.cc)|
1737
|401|[Binary Watch](https://leetcode.com/problems/binary-watch)|[binary-watch.cc](binary-watch.cc)|

Diff for: add-strings.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Add Strings
2+
class Solution {
3+
public:
4+
string addStrings(string a, string b) {
5+
string r;
6+
int m = a.size(), n = b.size(), c = 0;
7+
for (int i = 0; i < max(m, n); i++) {
8+
c += (i < m ? a[m-1-i]-'0' : 0) + (i < n ? b[n-1-i]-'0' : 0);
9+
r += '0'+c%10;
10+
c /= 10;
11+
}
12+
if (c)
13+
r += '1';
14+
reverse(r.begin(), r.end());
15+
return r;
16+
}
17+
};

Diff for: arithmetic-slices.cc

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

Diff for: battleships-in-a-board.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Battleships in a Board
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 countBattleships(vector<vector<char>>& a) {
8+
int r = 0;
9+
REP(i, a.size())
10+
REP(j, a[0].size())
11+
if (a[i][j] == 'X' && (! i || a[i-1][j] != 'X') && (! j || a[i][j-1] != 'X'))
12+
r++;
13+
return r;
14+
}
15+
};

Diff for: convert-a-number-to-hexadecimal.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Convert a Number to Hexadecimal
2+
class Solution {
3+
public:
4+
string toHex(int num) {
5+
string r;
6+
unsigned x = num;
7+
while (r += x%16 < 10 ? '0'+x%16 : 'a'+x%16-10, x /= 16);
8+
reverse(r.begin(), r.end());
9+
return r;
10+
}
11+
};

Diff for: find-all-anagrams-in-a-string.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Find All Anagrams in a String
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+
vector<int> findAnagrams(string s, string p) {
8+
vector<int> r;
9+
if (s.size() < p.size()) return r;
10+
int c[26] = {}, d = 0;
11+
for (char i: p)
12+
c[i-'a']--;
13+
REP(i, p.size())
14+
c[s[i]-'a']++;
15+
for (int i: c)
16+
if (i)
17+
d++;
18+
if (! d)
19+
r.push_back(0);
20+
REP(i, s.size()-p.size()) {
21+
if (! c[s[i+p.size()]-'a']++) d++;
22+
if (! c[s[i+p.size()]-'a']) d--;
23+
if (! c[s[i]-'a']--) d++;
24+
if (! c[s[i]-'a']) d--;
25+
if (! d) r.push_back(i+1);
26+
}
27+
return r;
28+
}
29+
};

Diff for: find-right-interval.cc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Find Right Interval
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+
vector<int> findRightInterval(vector<Interval>& intervals) {
8+
vector<int> r;
9+
vector<pair<int, int>> a;
10+
REP(i, intervals.size())
11+
a.emplace_back(intervals[i].start, i);
12+
sort(a.begin(), a.end());
13+
for (auto& x: intervals) {
14+
auto it = lower_bound(a.begin(), a.end(), make_pair(x.end, 0));
15+
r.push_back(it == a.end() ? -1 : it->second);
16+
}
17+
return r;
18+
}
19+
};

Diff for: fizz-buzz.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Fizz Buzz
2+
class Solution {
3+
public:
4+
vector<string> fizzBuzz(int n) {
5+
int fizz = 3, buzz = 5, fizzbuzz = 15;
6+
vector<string> r;
7+
for (int i = 1; i <= n; i++)
8+
if (i == fizzbuzz)
9+
r.push_back("FizzBuzz"), fizz += 3, buzz += 5, fizzbuzz += 15;
10+
else if (i == fizz)
11+
r.push_back("Fizz"), fizz += 3;
12+
else if (i == buzz)
13+
r.push_back("Buzz"), buzz += 5;
14+
else
15+
r.push_back(to_string(i));
16+
return r;
17+
}
18+
};

Diff for: longest-palindrome.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Longest Palindrome
2+
class Solution {
3+
public:
4+
int longestPalindrome(string s) {
5+
int c[52] = {}, d = 0, r = 0;
6+
for (char i: s)
7+
c[i >= 'a' ? i-'a' : i-'A'+26]++;
8+
for (int i: c) {
9+
d |= i%2;
10+
r += i-i%2;
11+
}
12+
return r+d;
13+
}
14+
};

Diff for: maximum-xor-of-two-numbers-in-an-array.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Maximum XOR of Two Numbers in an Array
2+
class Solution {
3+
public:
4+
int findMaximumXOR(vector<int>& nums) {
5+
int m = 0, r = 0;
6+
unordered_set<int> s;
7+
for (int i = 30; i >= 0; i--) {
8+
m |= 1<<i;
9+
s.clear();
10+
for (int x: nums)
11+
s.insert(x & m);
12+
int t = r|1<<i;
13+
for (int x: s)
14+
if (s.count(x^t)) {
15+
r = t;
16+
break;
17+
}
18+
}
19+
return r;
20+
}
21+
};

Diff for: minimum-moves-to-equal-array-elements.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Minimum Moves to Equal Array Elements
2+
class Solution {
3+
public:
4+
int minMoves(vector<int>& a) {
5+
return accumulate(a.begin(), a.end(), 0L)-*min_element(a.begin(), a.end())*a.size();
6+
}
7+
};

Diff for: non-overlapping-intervals.cc

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Non-overlapping Intervals
2+
class Solution {
3+
public:
4+
int eraseOverlapIntervals(vector<Interval>& a) {
5+
sort(a.begin(), a.end(), [](const Interval& x, const Interval& y) {
6+
return x.end < y.end;
7+
});
8+
int r = 0, y = INT_MIN;
9+
for (auto& x: a)
10+
if (y <= x.start) {
11+
y = x.end;
12+
r++;
13+
}
14+
return a.size()-r;
15+
}
16+
};

Diff for: number-of-boomerangs.cc

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Number of Boomerangs
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 numberOfBoomerangs(vector<pair<int, int>>& a) {
8+
int r = 0;
9+
REP(i, a.size()) {
10+
vector<int> d;
11+
REP(j, a.size())
12+
if (j != i)
13+
d.push_back((a[i].first-a[j].first)*
14+
(a[i].first-a[j].first)+
15+
(a[i].second-a[j].second)*
16+
(a[i].second-a[j].second));
17+
sort(d.begin(), d.end());
18+
for (int k = 0, j = 0; j < d.size(); j = k) {
19+
for (; k < d.size() && d[j] == d[k]; k++);
20+
r += (k-j)*(k-j-1);
21+
}
22+
}
23+
return r;
24+
}
25+
};

Diff for: pacific-atlantic-water-flow.cc

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Pacific Atlantic Water Flow
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+
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
8+
if (matrix.empty())
9+
return {};
10+
int m = matrix.size(), n = matrix[0].size();
11+
queue<pair<int, int>> q;
12+
vector<vector<char>> a(m, vector<char>(n, 0));
13+
auto bfs = [&](int b) {
14+
while (q.size()) {
15+
int x, y;
16+
tie(x, y) = q.front();
17+
q.pop();
18+
if (x && ! (a[x-1][y] & b) && matrix[x][y] <= matrix[x-1][y])
19+
a[x-1][y] |= b, q.emplace(x-1, y);
20+
if (x+1 < m && ! (a[x+1][y] & b) && matrix[x][y] <= matrix[x+1][y])
21+
a[x+1][y] |= b, q.emplace(x+1, y);
22+
if (y && ! (a[x][y-1] & b) && matrix[x][y] <= matrix[x][y-1])
23+
a[x][y-1] |= b, q.emplace(x, y-1);
24+
if (y+1 < n && ! (a[x][y+1] & b) && matrix[x][y] <= matrix[x][y+1])
25+
a[x][y+1] |= b, q.emplace(x, y+1);
26+
}
27+
};
28+
REP(i, m) {
29+
q.emplace(i, 0);
30+
a[i][0] = 1;
31+
}
32+
FOR(i, 1, n) {
33+
q.emplace(0, i);
34+
a[0][i] = 1;
35+
}
36+
bfs(1);
37+
REP(i, m) {
38+
q.emplace(i, n-1);
39+
a[i][n-1] |= 2;
40+
}
41+
REP(i, n-1) {
42+
q.emplace(m-1, i);
43+
a[m-1][i] |= 2;
44+
}
45+
bfs(2);
46+
vector<pair<int, int>> r;
47+
REP(i, m)
48+
REP(j, n)
49+
if (a[i][j] == 3)
50+
r.emplace_back(i, j);
51+
return r;
52+
}
53+
};

Diff for: partition-equal-subset-sum.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Partition Equal Subset Sum
2+
class Solution {
3+
public:
4+
bool canPartition(vector<int>& nums) {
5+
int s = accumulate(nums.begin(), nums.end(), 0);
6+
vector<char> a(s/2+1);
7+
a[0] = 1;
8+
for (int x: nums)
9+
for (int i = a.size(); --i >= x; )
10+
a[i] |= a[i-x];
11+
return s%2 == 0 && a[s/2];
12+
}
13+
};

Diff for: path-sum-iii.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Path Sum III
2+
class Solution {
3+
int sum, r = 0;
4+
unordered_map<int, int> m;
5+
void f(TreeNode* x, int s) {
6+
if (! x) return;
7+
s += x->val;
8+
r += m[s-sum];
9+
m[s]++;
10+
f(x->left, s);
11+
f(x->right, s);
12+
m[s]--;
13+
}
14+
public:
15+
int pathSum(TreeNode* root, int sum) {
16+
this->sum = sum;
17+
m[0] = 1;
18+
f(root, 0);
19+
return r;
20+
}
21+
};
22+

Diff for: queue-reconstruction-by-height.cc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Queue Reconstruction by Height
2+
class Solution {
3+
public:
4+
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
5+
vector<pair<int, int>> r(people.size());
6+
for (auto& p: people)
7+
p.second *= -1;
8+
sort(people.begin(), people.end());
9+
vector<int> fenwick(people.size());
10+
for (int i = 0; i < people.size(); i++)
11+
fenwick[i] = i+1 & ~ i;
12+
for (auto& p: people) {
13+
p.second *= -1;
14+
int c = 0, x = -1;
15+
for (int k = 1 << 31-__builtin_clz(people.size()); k; k >>= 1)
16+
if (x+k < people.size() && c+fenwick[x+k] <= p.second)
17+
c += fenwick[x += k];
18+
r[++x] = p;
19+
for (; x < people.size(); x |= x+1)
20+
fenwick[x]--;
21+
}
22+
return r;
23+
}
24+
};

Diff for: split-array-largest-sum.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Split Array Largest Sum
2+
class Solution {
3+
public:
4+
int splitArray(vector<int>& a, int m) {
5+
long l = 0, h = 0;
6+
for (long i: a) {
7+
l = max(l, i);
8+
h += i;
9+
}
10+
while (l < h) {
11+
long x = l+h >> 1, s = 0, n = m;
12+
for (long i: a)
13+
if ((s += i) > x && (s = i, --n <= 0))
14+
break;
15+
if (n <= 0)
16+
l = x+1;
17+
else
18+
h = x;
19+
}
20+
return l;
21+
}
22+
};

0 commit comments

Comments
 (0)