Skip to content

Commit 2fc46fa

Browse files
committed
Add 1874~1897
1 parent d05b888 commit 2fc46fa

10 files changed

+205
-5
lines changed

README.md

+11-4
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 482/559 problems.
5+
Solved 489/1771 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|1897|[Maximize Palindrome Length From Subsequences](https://leetcode.com/problems/maximize-palindrome-length-from-subsequences)|[maximize-palindrome-length-from-subsequences.cc](maximize-palindrome-length-from-subsequences.cc)|
16+
|1896|[Maximum Score from Performing Multiplication Operations](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations)|[maximum-score-from-performing-multiplication-operations.cc](maximum-score-from-performing-multiplication-operations.cc)|
17+
|1895|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box)|[minimum-number-of-operations-to-move-all-balls-to-each-box.cc](minimum-number-of-operations-to-move-all-balls-to-each-box.cc)|
18+
|1894|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately)|[merge-strings-alternately.cc](merge-strings-alternately.cc)|
19+
|1876|[Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak)|[map-of-highest-peak.cc](map-of-highest-peak.cc)|
20+
|1875|[Tree of Coprimes](https://leetcode.com/problems/tree-of-coprimes)|[tree-of-coprimes.cc](tree-of-coprimes.cc)|
21+
|1874|[Form Array by Concatenating Subarrays of Another Array](https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array)|[form-array-by-concatenating-subarrays-of-another-array.cc](form-array-by-concatenating-subarrays-of-another-array.cc)|
22+
|916|[Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index)|[decoded-string-at-index.cc](decoded-string-at-index.cc)|
23+
|830|[Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area)|[largest-triangle-area.cc](largest-triangle-area.cc)|
24+
|673|[Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence)|[number-of-longest-increasing-subsequence.cc](number-of-longest-increasing-subsequence.cc)|
1525
|635|[Design Log Storage System](https://leetcode.com/problems/design-log-storage-system)|[design-log-storage-system.cc](design-log-storage-system.cc)|
1626
|634|[Find the Derangement of An Array](https://leetcode.com/problems/find-the-derangement-of-an-array)|[find-the-derangement-of-an-array.cc](find-the-derangement-of-an-array.cc)|
1727
|633|[Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers)|[sum-of-square-numbers.cc](sum-of-square-numbers.cc)|
@@ -149,7 +159,6 @@ See [database.md](database.md)
149159
|366|[Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree)|[find-leaves-of-binary-tree.cc](find-leaves-of-binary-tree.cc)|
150160
|365|[Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem)|[water-and-jug-problem.cc](water-and-jug-problem.cc)|
151161
|364|[Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii)|[nested-list-weight-sum-ii.cc](nested-list-weight-sum-ii.cc)|
152-
|363|[Max Sum of Rectangle No Larger Than K](https://leetcode.com/problems/max-sum-of-sub-matrix-no-larger-than-k)|[max-sum-of-sub-matrix-no-larger-than-k.cc](max-sum-of-sub-matrix-no-larger-than-k.cc)|
153162
|362|[Design Hit Counter](https://leetcode.com/problems/design-hit-counter)|[design-hit-counter.cc](design-hit-counter.cc)|
154163
|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy)|[bomb-enemy.cc](bomb-enemy.cc)|
155164
|360|[Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array)|[sort-transformed-array.cc](sort-transformed-array.cc)|
@@ -300,7 +309,6 @@ See [database.md](database.md)
300309
|214|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome)|[shortest-palindrome.cc](shortest-palindrome.cc)|
301310
|213|[House Robber II](https://leetcode.com/problems/house-robber-ii)|[house-robber-ii.cc](house-robber-ii.cc)|
302311
|212|[Word Search II](https://leetcode.com/problems/word-search-ii)|[word-search-ii.cc](word-search-ii.cc)|
303-
|211|[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design)|[add-and-search-word-data-structure-design.cc](add-and-search-word-data-structure-design.cc)|
304312
|210|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii)|[course-schedule-ii.cc](course-schedule-ii.cc)|
305313
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum)|[minimum-size-subarray-sum.cc](minimum-size-subarray-sum.cc)|
306314
|208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree)|[implement-trie-prefix-tree.cc](implement-trie-prefix-tree.cc)|
@@ -460,7 +468,6 @@ See [database.md](database.md)
460468
|37|[Sudoku Solver](https://leetcode.com/problems/sudoku-solver)|[sudoku-solver.cc](sudoku-solver.cc)|
461469
|36|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku)|[valid-sudoku.cc](valid-sudoku.cc)|
462470
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position)|[search-insert-position.cc](search-insert-position.cc)|
463-
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range)|[search-for-a-range.cc](search-for-a-range.cc)|
464471
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array)|[search-in-rotated-sorted-array.cc](search-in-rotated-sorted-array.cc)|
465472
|32|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses)|[longest-valid-parentheses.cc](longest-valid-parentheses.cc)|
466473
|31|[Next Permutation](https://leetcode.com/problems/next-permutation)|[next-permutation.cc](next-permutation.cc)|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Form Array by Concatenating Subarrays of Another Array
2+
class Solution {
3+
public:
4+
bool canChoose(vector<vector<int>>& gs, vector<int>& a) {
5+
int n = a.size(), f = 0;
6+
vector<int> pi(n);
7+
for (auto &g : gs) {
8+
int m = g.size();
9+
for (int j = 0, i = 1; i < m; i++) {
10+
while (j && g[i] != g[j]) j = pi[j-1];
11+
if (g[i] == g[j]) j++;
12+
pi[i] = j;
13+
}
14+
int j = 0;
15+
while (f < n) {
16+
while (j && a[f] != g[j]) j = pi[j-1];
17+
if (a[f++] == g[j] && ++j == m)
18+
break;
19+
}
20+
if (j != m)
21+
return false;
22+
}
23+
return true;
24+
}
25+
};

fraction-addition-and-subtraction.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Fraction Addition and Subtraction
22
class Solution {
3+
int gcd(int a, int b) {
4+
int t;
5+
while (b)
6+
t = a%b, a = b, b = t;
7+
return a;
8+
}
39
public:
410
string fractionAddition(string s) {
511
int p = 0, q = 1, p1, q1, t;
@@ -10,9 +16,10 @@ class Solution {
1016
int x = stol(s.substr(i, k-i)), y = stol(s.substr(k+1, j));
1117
p1 = p*y+q*x;
1218
q1 = q*y;
13-
t = __gcd(abs(p1), q1);
19+
t = gcd(p1, q1);
1420
p = p1/t;
1521
q = q1/t;
22+
if (q < 0) p *= -1, q *= -1;
1623
}
1724
return to_string(p)+"/"+to_string(q);
1825
}

map-of-highest-peak.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Map of Highest Peak
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<vector<int>> highestPeak(vector<vector<int>>& a) {
8+
int m = a.size(), n = a[0].size(), r, c;
9+
vector<pair<int, int>> q;
10+
REP(i, m) REP(j, n)
11+
if (a[i][j]--)
12+
q.emplace_back(i, j);
13+
REP(i, q.size()) {
14+
tie(r, c) = q[i];
15+
int t = max(a[r][c]+1, 1);
16+
if (r && a[r-1][c]<0) a[r-1][c]=t, q.emplace_back(r-1, c);
17+
if (r+1 < m && a[r+1][c]<0) a[r+1][c]=t, q.emplace_back(r+1, c);
18+
if (c && a[r][c-1]<0) a[r][c-1]=t, q.emplace_back(r, c-1);
19+
if (c+1 < n && a[r][c+1]<0) a[r][c+1]=t, q.emplace_back(r, c+1);
20+
}
21+
return a;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Maximize Palindrome Length From Subsequences
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
int longestPalindrome(string a, string b) {
8+
string c = a+b;
9+
int n = c.size(), na = a.size(), ans = 0;
10+
vector<vector<int>> s(n, vector<int>(n));
11+
ROF(i, 0, n) {
12+
s[i][i] = 1;
13+
FOR(j, i+1, n) {
14+
if (c[i] == c[j]) {
15+
s[i][j] = s[i+1][j-1]+2;
16+
if (i < na && na <= j)
17+
ans = max(ans, s[i][j]);
18+
} else
19+
s[i][j] = max(s[i+1][j], s[i][j-1]);
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Maximum Score from Performing Multiplication Operations
2+
class Solution {
3+
public:
4+
int maximumScore(vector<int>& a, vector<int>& b) {
5+
int n = a.size(), m = b.size();
6+
vector<vector<int>> f(m+1, vector<int>(m+1));
7+
for (int i = m; i--; )
8+
for (int j = m-i; --j >= 0; )
9+
f[i][j] = max(f[i+1][j]+a[i]*b[i+j], f[i][j+1]+a[n-1-j]*b[i+j]);
10+
return f[0][0];
11+
}
12+
};

merge-strings-alternately.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Merge Strings Alternately
2+
class Solution {
3+
public:
4+
string mergeAlternately(string a, string b) {
5+
string s;
6+
int n = min(a.size(), b.size());
7+
for (int i = 0; i < n; i++)
8+
(s += a[i]) += b[i];
9+
return s+a.substr(n)+b.substr(n);
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Minimum Number of Operations to Move All Balls to Each Box
2+
class Solution {
3+
public:
4+
vector<int> minOperations(string b) {
5+
int n = b.size(), ff = 0, gg = 0;
6+
vector<int> f(n+1), g(n+1);
7+
for (int i = n; i--; ) {
8+
f[i] = f[i+1]+i*(b[i]-'0');
9+
g[i] = g[i+1]+(b[i]-'0');
10+
}
11+
for (int i = 0; i < n; i++) {
12+
f[i] = f[i+1]-g[i+1]*i + gg*i-ff;
13+
ff += i*(b[i]-'0');
14+
gg += b[i]-'0';
15+
}
16+
f.pop_back();
17+
return f;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
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 cutOffTree(vector<vector<int>>& g) {
8+
int m = g.size(), n = g[0].size(), r = 0, sx = 0, sy = 0, x, y;
9+
int d[50][50];
10+
pair<int, int> q[2500];
11+
for(;;) {
12+
int tx = -1, ty = -1;
13+
REP(i, m) REP(j, n)
14+
if (g[i][j] > 1 && (tx<0 || g[i][j] < g[tx][ty]))
15+
tx = i, ty = j;
16+
if (tx < 0) break;
17+
REP(i, m)
18+
fill_n(d[i], n, -1);
19+
d[sx][sy] = 0;
20+
q[0] = make_pair(sx, sy);
21+
for (int l = 0, h = 1; l < h && d[tx][ty] < 0; l++) {
22+
tie(x, y) = q[l];
23+
if (x && g[x-1][y] && d[x-1][y] < 0) d[x-1][y] = d[x][y]+1, q[h++] = {x-1, y};
24+
if (x+1 < m && g[x+1][y] && d[x+1][y] < 0) d[x+1][y] = d[x][y]+1, q[h++] = {x+1, y};
25+
if (y && g[x][y-1] && d[x][y-1] < 0) d[x][y-1] = d[x][y]+1, q[h++] = {x, y-1};
26+
if (y+1 < n && g[x][y+1] && d[x][y+1] < 0) d[x][y+1] = d[x][y]+1, q[h++] = {x, y+1};
27+
}
28+
if (d[tx][ty] < 0) return -1;
29+
g[tx][ty] = 1;
30+
r += d[tx][ty];
31+
sx = tx, sy = ty;
32+
}
33+
return r;
34+
}
35+
};

tree-of-coprimes.cc

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Tree of Coprimes
2+
class Solution {
3+
vector<int> co[51];
4+
vector<pair<int, int>> st[51];
5+
vector<int> nums, ans;
6+
vector<vector<int>> e;
7+
void dfs(int u, int dep, int p) {
8+
int x = nums[u], maxd = -1;
9+
for (int y : co[x]) {
10+
auto &c = st[y];
11+
if (c.size() && c.back().first > maxd)
12+
tie(maxd, ans[u]) = c.back();
13+
}
14+
st[x].emplace_back(dep, u);
15+
for (int v : e[u])
16+
if (v != p)
17+
dfs(v, dep+1, u);
18+
st[x].pop_back();
19+
}
20+
public:
21+
vector<int> getCoprimes(vector<int>& nums, vector<vector<int>>& edges) {
22+
this->nums = nums;
23+
ans.assign(nums.size(), -1);
24+
e.resize(nums.size());
25+
set<int> n(nums.begin(), nums.end());
26+
for (int a : n)
27+
for (int b : n)
28+
if (__gcd(a, b) == 1)
29+
co[a].push_back(b);
30+
for (auto &i : edges) {
31+
e[i[0]].push_back(i[1]);
32+
e[i[1]].push_back(i[0]);
33+
}
34+
dfs(0, 0, -1);
35+
return ans;
36+
}
37+
};

0 commit comments

Comments
 (0)