Skip to content

Commit e19ad41

Browse files
committed
298/298
1 parent 416bb8f commit e19ad41

10 files changed

+345
-1
lines changed

README.md

+10-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 289/290 problems.
5+
Solved 298/298 problems.
66

77
## Database
88

@@ -12,7 +12,16 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)|[count-of-smaller-numbers-after-self.cc](count-of-smaller-numbers-after-self.cc)|
16+
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[binary-tree-vertical-order-traversal.cc](binary-tree-vertical-order-traversal.cc)|
17+
|313|[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/)|[super-ugly-number.cc](super-ugly-number.cc)|
18+
|312|[Burst Balloons](https://leetcode.com/problems/burst-balloons/)|[burst-balloons.cc](burst-balloons.cc)|
19+
|311|[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)|[sparse-matrix-multiplication.cc](sparse-matrix-multiplication.cc)|
20+
|310|[Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/)|[minimum-height-trees.cc](minimum-height-trees.cc)|
21+
|309|[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)|[best-time-to-buy-and-sell-stock-with-cooldown.cc](best-time-to-buy-and-sell-stock-with-cooldown.cc)|
22+
|308|[Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/)|[range-sum-query-2d-mutable.cc](range-sum-query-2d-mutable.cc)|
1523
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/)|[range-sum-query-mutable.cc](range-sum-query-mutable.cc)|
24+
|306|[Additive Number](https://leetcode.com/problems/additive-number/)|[additive-number.cc](additive-number.cc)|
1625
|305|[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)|[number-of-islands-ii.cc](number-of-islands-ii.cc)|
1726
|304|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)|[range-sum-query-2d-immutable.cc](range-sum-query-2d-immutable.cc)|
1827
|303|[Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/)|[range-sum-query-immutable.cc](range-sum-query-immutable.cc)|

additive-number.cc

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Additive Number
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
bool isAdditiveNumber(string num) {
8+
int n = num.size();
9+
ROF(u, 1, n)
10+
if (num[u] != '0' || u == n-1)
11+
FOR(v, max(0, 2*u-n), u)
12+
if (num[v] != '0' || v == u-1) {
13+
for (int p = n, q = u, r = v; ; ) {
14+
int i = p, j = q, k = r, zero = 0, b = 0;
15+
for (; i > q; i--) {
16+
int t = num[i-1] - (j > r ? num[--j] : '0') - b;
17+
if (t < 0)
18+
t += 10, b = 1;
19+
else
20+
b = 0;
21+
if (! t)
22+
zero++;
23+
else {
24+
for (; zero; zero--)
25+
if (! k || num[--k] != '0')
26+
goto out;
27+
if (! k || num[--k]-'0' != t)
28+
goto out;
29+
}
30+
}
31+
if (k == r && (! k || num[--k] != '0'))
32+
goto out;
33+
if (! k) {
34+
if (j == r) return true;
35+
goto out;
36+
}
37+
p = q;
38+
q = r;
39+
r = k;
40+
}
41+
out:;
42+
}
43+
return false;
44+
}
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Best Time to Buy and Sell Stock with Cooldown
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
4+
class Solution {
5+
public:
6+
int maxProfit(vector<int>& prices) {
7+
if (prices.empty()) return 0;
8+
int f = - prices[0], g = 0, gg = 0;
9+
FOR(i, 1, prices.size()) {
10+
int t = gg;
11+
gg = g;
12+
g = max(g, f+prices[i]);
13+
f = max(f, t-prices[i]);
14+
}
15+
return g;
16+
}
17+
};
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Binary Tree Vertical Order Traversal
2+
class Solution {
3+
public:
4+
vector<vector<int>> verticalOrder(TreeNode* root) {
5+
vector<vector<int>> r;
6+
if (root) {
7+
queue<pair<TreeNode*, int>> q;
8+
map<int, vector<int>> a;
9+
q.emplace(root, 0);
10+
while (! q.empty()) {
11+
auto x = q.front();
12+
q.pop();
13+
a[x.second].push_back(x.first->val);
14+
if (x.first->left)
15+
q.emplace(x.first->left, x.second-1);
16+
if (x.first->right)
17+
q.emplace(x.first->right, x.second+1);
18+
}
19+
for (auto &i: a)
20+
r.push_back(i.second);
21+
}
22+
return r;
23+
}
24+
};

burst-balloons.cc

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Burst Balloons
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
5+
6+
class Solution {
7+
public:
8+
int maxCoins(vector<int>& nums) {
9+
int n = nums.size();
10+
if (! n)
11+
return 0;
12+
vector<vector<int>> dp(n, vector<int>(n, 0));
13+
REP(i, n)
14+
dp[i][i] = (i ? nums[i-1] : 1) * nums[i] * (i+1 < n ? nums[i+1] : 1);
15+
ROF(i, 0, n-1)
16+
FOR(j, i+1, n) {
17+
int opt = -1, optk;
18+
FOR(k, i, j+1) {
19+
int t = (k ? dp[i][k-1] : 0) + (k+1 < n ? dp[k+1][j] : 0) + (i ? nums[i-1] : 1) * nums[k] * (j+1 < n ? nums[j+1] : 1);
20+
if (t > opt)
21+
opt = t;
22+
}
23+
dp[i][j] = opt;
24+
}
25+
return dp[0][n-1];
26+
}
27+
};
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Count of Smaller Numbers After Self
2+
// divide and conquer
3+
typedef pair<int, int> pii;
4+
#define REP(i, n) for (int i = 0; i < (n); i++)
5+
6+
class Solution {
7+
vector<pii> a, b;
8+
vector<int> c;
9+
void f(int l, int r) {
10+
if (r-l <= 1)
11+
return;
12+
int m = l+(r-l)/2;
13+
f(l, m);
14+
f(m, r);
15+
for (int i = l, j = m, k = l; i < m || j < r; )
16+
if (j == r || i < m && a[i].first <= a[j].first) {
17+
c[a[i].second] += j-m;
18+
b[k++] = a[i++];
19+
} else
20+
b[k++] = a[j++];
21+
copy(b.begin()+l, b.begin()+r, a.begin()+l);
22+
}
23+
public:
24+
vector<int> countSmaller(vector<int>& nums) {
25+
int n = nums.size();
26+
a.resize(n);
27+
b.resize(n);
28+
c.assign(n, 0);
29+
REP(i, n)
30+
a[i] = pii{nums[i], i};
31+
f(0, n);
32+
return c;
33+
}
34+
};
35+
36+
// order statistics tree
37+
38+
#include <ext/pb_ds/assoc_container.hpp>
39+
#include <ext/pb_ds/tree_policy.hpp>
40+
using namespace __gnu_pbds;
41+
42+
typedef pair<int, int> pii;
43+
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
44+
45+
class Solution {
46+
public:
47+
vector<int> countSmaller(vector<int>& nums) {
48+
tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> tr;
49+
ROF(i, 0, nums.size()) {
50+
tr.insert(pii{nums[i], i});
51+
nums[i] = tr.order_of_key(pii{nums[i], 0});
52+
}
53+
return nums;
54+
}
55+
};

minimum-height-trees.cc

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Minimum Height Trees
2+
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
8+
vector<vector<int>> es(n);
9+
vector<int> d(n, 0);
10+
vector<bool> removed(n, false);
11+
for (auto &e: edges) {
12+
es[e.first].push_back(e.second);
13+
es[e.second].push_back(e.first);
14+
d[e.first]++;
15+
d[e.second]++;
16+
}
17+
vector<int> leaves;
18+
REP(i, n)
19+
if (d[i] <= 1)
20+
leaves.push_back(i);
21+
while (n > 2) {
22+
vector<int> leaves2;
23+
n -= leaves.size();
24+
for (int u: leaves) {
25+
int p = -1;
26+
for (int v: es[u])
27+
if (! removed[v])
28+
p = v;
29+
removed[u] = true;
30+
if (--d[p] == 1)
31+
leaves2.push_back(p);
32+
}
33+
leaves.swap(leaves2);
34+
}
35+
return leaves;
36+
}
37+
};
38+
39+
/// find a diameter
40+
41+
class Solution {
42+
vector<vector<int>> es;
43+
vector<int> parent;
44+
int bfs(int u) {
45+
queue<int> q;
46+
int last = u;
47+
fill(parent.begin(), parent.end(), -1);
48+
parent[u] = -2;
49+
q.push(u);
50+
while (! q.empty()) {
51+
u = q.front();
52+
q.pop();
53+
last = u;
54+
for (int v: es[u])
55+
if (parent[v] == -1) {
56+
parent[v] = u;
57+
q.push(v);
58+
}
59+
}
60+
return last;
61+
}
62+
public:
63+
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
64+
es.resize(n);
65+
parent.resize(n);
66+
for (auto &e: edges) {
67+
es[e.first].push_back(e.second);
68+
es[e.second].push_back(e.first);
69+
}
70+
vector<int> ancestors;
71+
for (int v = bfs(bfs(0)); v != -2; v = parent[v])
72+
ancestors.push_back(v);
73+
vector<int> ret{ancestors[ancestors.size()/2]};
74+
if (ancestors.size() % 2 == 0)
75+
ret.push_back(ancestors[ancestors.size()/2-1]);
76+
return ret;
77+
}
78+
};

range-sum-query-2d-mutable.cc

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Range Sum Query - Mutable
2+
// 2D Fenwick tree
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
class NumMatrix {
6+
int m, n;
7+
vector<vector<int>> a;
8+
void add(int i, int j, int v) {
9+
for (; i < m; i |= i+1)
10+
for (int jj = j; jj < n; jj |= jj+1)
11+
a[i][jj] += v;
12+
}
13+
int sum(int i, int j) {
14+
int s = 0;
15+
for (; i; i &= i-1)
16+
for (int jj = j; jj; jj &= jj-1)
17+
s += a[i-1][jj-1];
18+
return s;
19+
}
20+
public:
21+
NumMatrix(vector<vector<int>> &matrix) {
22+
m = matrix.size();
23+
n = m ? matrix[0].size() : 0;
24+
a.assign(m, vector<int>(n, 0));
25+
REP(i, m)
26+
REP(j, n)
27+
add(i, j, matrix[i][j]);
28+
}
29+
void update(int row, int col, int val) {
30+
add(row, col, val - sumRegion(row, col, row, col));
31+
}
32+
int sumRegion(int row1, int col1, int row2, int col2) {
33+
return sum(row2+1, col2+1) - sum(row1, col2+1)
34+
- sum(row2+1, col1) + sum(row1, col1);
35+
}
36+
};

sparse-matrix-multiplication.cc

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Sparse Matrix Multiplication
2+
#define REP(i, n) for (int i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
7+
int m = A.size(), p = B.size(), n = B[0].size();
8+
unordered_map<int, unordered_map<int, int>> a, b;
9+
vector<vector<int>> C(m, vector<int>(n, 0));
10+
REP(i, m)
11+
REP(j, p)
12+
if (A[i][j])
13+
a[i][j] = A[i][j];
14+
REP(i, p)
15+
REP(j, n)
16+
if (B[i][j])
17+
b[j][i] = B[i][j];
18+
for (auto &iA: a) {
19+
int i = iA.first;
20+
auto &rowA = iA.second;
21+
for (auto &iB: b) {
22+
int j = iB.first;
23+
auto &colB = iB.second;
24+
for (auto x: rowA)
25+
if (colB.count(x.first))
26+
C[i][j] += x.second * colB[x.first];
27+
}
28+
}
29+
return C;
30+
}
31+
};

super-ugly-number.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Super Ugly Number
2+
typedef pair<int, int> pii;
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
int nthSuperUglyNumber(int n, vector<int>& primes) {
8+
int m = primes.size();
9+
vector<int> ugly(n, 1), idx(m, 0);
10+
priority_queue<pii, vector<pii>, greater<pii>> pq;
11+
REP(i, m)
12+
pq.emplace(primes[i], i);
13+
for (int i = 1; i < n; ) {
14+
pii x = pq.top();
15+
pq.pop();
16+
if (x.first != ugly[i-1])
17+
ugly[i++] = x.first;
18+
pq.emplace(ugly[++idx[x.second]] * primes[x.second], x.second);
19+
}
20+
return ugly[n-1];
21+
}
22+
};

0 commit comments

Comments
 (0)