Skip to content

Commit 231d46a

Browse files
committed
feb-28
1 parent 4928700 commit 231d46a

9 files changed

+424
-164
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5252
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5353
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5454
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
55-
| [🔲] | [9c-top.md](./list/9c-top.md) | 44/56 | 2 vips |
55+
| [🔲] | [9c-top.md](./list/9c-top.md) | 46/56 | 2 vips |
5656
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 40/2207 | 3 vips |
5757

58-
**Solved**: 561 problems
58+
**Solved**: 562 problems
5959

6060
## 类型/Category
6161

@@ -100,7 +100,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
100100

101101
## Math
102102

103-
| Link | Problem(30) | Solution | Tag | Time | Space | Ref |
103+
| Link | Problem(31) | Solution | Tag | Time | Space | Ref |
104104
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
105105
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
106106
| [Leetcode-504](https://leetcode.com/problems/base-7/) | Base 7 | [c++](./leetcode/504.base-7.cpp), [python3](./leetcode/504.base-7.py) | Math | O\(N\) | O\(1\) | - |
@@ -113,6 +113,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
113113
| [Leetcode-204](https://leetcode.com/problems/count-primes/) | Count Primes | [c++](./leetcode/204.count-primes.cpp), [python3](./leetcode/204.count-primes.py) | Math | O\(NlogN\) | O\(N\) | - |
114114
| [Leetcode-150](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Evaluate Reverse Polish Notation | [c++](./leetcode/150.evaluate-reverse-polish-notation.cpp), [python3](./leetcode/150.evaluate-reverse-polish-notation.py) | Math | O\(N\) | O\(N\) | - |
115115
| [Leetcode-168](https://leetcode.com/problems/excel-sheet-column-title/) | Excel Sheet Column Title | [c++](./leetcode/168.excel-sheet-column-title.cpp), [python3](./leetcode/168.excel-sheet-column-title.py) | Math | O\(logN\) | O\(1\) | - |
116+
| [Leetcode-282](https://leetcode.com/problems/expression-add-operators/) | Expression Add Operators | [c++](./leetcode/282.expression-add-operators.cpp), [python3](./leetcode/282.expression-add-operators.py) | Math | O\(2^N\) | O\(N\) | - |
116117
| [Leetcode-172](https://leetcode.com/problems/factorial-trailing-zeroes/) | Factorial Trailing Zeroes | [c++](./leetcode/172.factorial-trailing-zeroes.cpp), [python3](./leetcode/172.factorial-trailing-zeroes.py) | Math | O\(logN\) | O\(1\) | - |
117118
| [Leetcode-2269](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | Find The K Beauty Of A Number | [c++](./leetcode/2269.find-the-k-beauty-of-a-number.cpp), [python3](./leetcode/2269.find-the-k-beauty-of-a-number.py) | Math | O\(N\) | O\(N\) | - |
118119
| [Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | Find The Punishment Number Of An Integer | [c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py) | Math | O\(N \* 2^K\) | O\(K\) | - |
@@ -277,7 +278,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
277278

278279
## Dynamic Programming
279280

280-
| Link | Problem(78) | Solution | Tag | Time | Space | Ref |
281+
| Link | Problem(79) | Solution | Tag | Time | Space | Ref |
281282
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
282283
| [Leetcode-542](https://leetcode.com/problems/01-matrix/) | 01 Matrix | [c++](./leetcode/542.01-matrix.cpp), [python3](./leetcode/542.01-matrix.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
283284
| [Leetcode-650](https://leetcode.com/problems/2-keys-keyboard/) | 2 Keys Keyboard | [c++](./leetcode/650.2-keys-keyboard.cpp), [python3](./leetcode/650.2-keys-keyboard.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | - |
@@ -338,6 +339,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
338339
| [Leetcode-354](https://leetcode.com/problems/russian-doll-envelopes/) | Russian Doll Envelopes | [c++](./leetcode/354.russian-doll-envelopes.cpp), [python3](./leetcode/354.russian-doll-envelopes.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | - |
339340
| [Leetcode-87](https://leetcode.com/problems/scramble-string/) | Scramble String | [c++](./leetcode/87.scramble-string.cpp), [python3](./leetcode/87.scramble-string.py) | Dynamic Programming | O\(N^4\) | O\(N^3\) | - |
340341
| [Leetcode-3276](https://leetcode.com/problems/select-cells-in-grid-with-maximum-score/) | Select Cells In Grid With Maximum Score | [c++](./leetcode/3276.select-cells-in-grid-with-maximum-score.cpp), [python3](./leetcode/3276.select-cells-in-grid-with-maximum-score.py) | Dynamic Programming | O\(NM\*2^N\) | O\(2^N\) | - |
342+
| [Leetcode-1092](https://leetcode.com/problems/shortest-common-supersequence/) | Shortest Common Supersequence | [c++](./leetcode/1092.shortest-common-supersequence.cpp), [python3](./leetcode/1092.shortest-common-supersequence.py) | Dynamic Programming | O\(NM\) | O\(NM\) | - |
341343
| [Leetcode-313](https://leetcode.com/problems/super-ugly-number/) | Super Ugly Number | [c++](./leetcode/313.super-ugly-number.cpp), [python3](./leetcode/313.super-ugly-number.py) | Dynamic Programming | O\(NlogK\) | O\(N\+K\) | - |
342344
| [Leetcode-494](https://leetcode.com/problems/target-sum/) | Target Sum | [c++](./leetcode/494.target-sum.cpp), [python3](./leetcode/494.target-sum.py) | Dynamic Programming | O\(N \* Sum\) | O\(N \* Sum\) | - |
343345
| [Leetcode-42](https://leetcode.com/problems/trapping-rain-water/) | Trapping Rain Water | [c++](./leetcode/42.trapping-rain-water.cpp), [python3](./leetcode/42.trapping-rain-water.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
@@ -904,7 +906,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
904906

905907
## Depth-First Search
906908

907-
| Link | Problem(59) | Solution | Tag | Time | Space | Ref |
909+
| Link | Problem(60) | Solution | Tag | Time | Space | Ref |
908910
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
909911
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Depth-First Search | O\(MlogM\) | O\(N\) | - |
910912
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | - |
@@ -962,6 +964,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
962964
| [Leetcode-653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | Two Sum Iv Input Is A Bst | [c++](./leetcode/653.two-sum-iv-input-is-a-bst.cpp), [python3](./leetcode/653.two-sum-iv-input-is-a-bst.py) | Depth-First Search | O\(NH\) | O\(H\) | - |
963965
| [Leetcode-98](https://leetcode.com/problems/validate-binary-search-tree/) | Validate Binary Search Tree | [c++](./leetcode/98.validate-binary-search-tree.cpp), [python3](./leetcode/98.validate-binary-search-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
964966
| [Lintcode-649](https://www.lintcode.com/problem/binary-tree-upside-down/) | Binary Tree Upside Down | [c++](./lintcode/649.binary-tree-upside-down.cpp), [python3](./lintcode/649.binary-tree-upside-down.py) | Depth-First Search | O\(N\) | O\(H\) | Leetcode-156 |
967+
| [Lintcode-652](https://www.lintcode.com/problem/factorization/) | Factorization | [c++](./lintcode/652.factorization.cpp), [python3](./lintcode/652.factorization.py) | Depth-First Search | O\(2^K\) | O\(LogN\) | - |
965968
| [Lintcode-650](https://www.lintcode.com/problem/find-leaves-of-binary-tree/) | Find Leaves Of Binary Tree | [c++](./lintcode/650.find-leaves-of-binary-tree.cpp), [python3](./lintcode/650.find-leaves-of-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | Leetcode-366 |
966969
| [Lintcode-11](https://www.lintcode.com/problem/search-range-in-binary-search-tree/) | Search Range In Binary Search Tree | [c++](./lintcode/11.search-range-in-binary-search-tree.cpp), [python3](./lintcode/11.search-range-in-binary-search-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
967970
| [Lintcode-634](https://www.lintcode.com/problem/word-squares/) | Word Squares | [c++](./lintcode/634.word-squares.cpp), [python3](./lintcode/634.word-squares.py) | Depth-First Search | O\(N \* P^5\) | O\(N\) | Leetcode-425 |
@@ -988,13 +991,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
988991

989992
## Backtracking
990993

991-
| Link | Problem(21) | Solution | Tag | Time | Space | Ref |
994+
| Link | Problem(22) | Solution | Tag | Time | Space | Ref |
992995
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
993996
| [Leetcode-257](https://leetcode.com/problems/binary-tree-paths/) | Binary Tree Paths | [c++](./leetcode/257.binary-tree-paths.cpp), [python3](./leetcode/257.binary-tree-paths.py) | Backtracking | O\(N\) | O\(H\) | - |
994997
| [Leetcode-40](https://leetcode.com/problems/combination-sum-ii/) | Combination Sum II | [c++](./leetcode/40.combination-sum-ii.cpp), [python3](./leetcode/40.combination-sum-ii.py) | Backtracking | O\(2^N\) | O\(N\) | - |
995998
| [Leetcode-77](https://leetcode.com/problems/combinations/) | Combinations | [c++](./leetcode/77.combinations.cpp), [python3](./leetcode/77.combinations.py) | Backtracking | O\(k \* C\(n, k\)\) | O\(K\) | - |
996999
| [Leetcode-2375](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | Construct Smallest Number From Di String | [c++](./leetcode/2375.construct-smallest-number-from-di-string.cpp), [python3](./leetcode/2375.construct-smallest-number-from-di-string.py) | Backtracking | O\(N\) | O\(N\) | - |
9971000
| [Leetcode-1718](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/) | Construct The Lexicographically Largest Valid Sequence | [c++](./leetcode/1718.construct-the-lexicographically-largest-valid-sequence.cpp), [python3](./leetcode/1718.construct-the-lexicographically-largest-valid-sequence.py) | Backtracking | O\(N\!\) | O\(N\) | - |
1001+
| [Leetcode-282](https://leetcode.com/problems/expression-add-operators/) | Expression Add Operators | [c++](./leetcode/282.expression-add-operators.cpp), [python3](./leetcode/282.expression-add-operators.py) | Backtracking | O\(2^N\) | O\(N\) | - |
9981002
| [Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | Find The Punishment Number Of An Integer | [c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py) | Backtracking | O\(N \* 2^K\) | O\(K\) | - |
9991003
| [Leetcode-1980](https://leetcode.com/problems/find-unique-binary-string/) | Find Unique Binary String | [c++](./leetcode/1980.find-unique-binary-string.cpp), [python3](./leetcode/1980.find-unique-binary-string.py) | Backtracking | O\(N\) | O\(1\) | - |
10001004
| [Leetcode-22](https://leetcode.com/problems/generate-parentheses/) | Generate Parentheses | [c++](./leetcode/22.generate-parentheses.cpp), [python3](./leetcode/22.generate-parentheses.py) | Backtracking | O\(2^N\) | O\(N\) | - |
@@ -1146,7 +1150,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
11461150

11471151
## Other
11481152

1149-
| Link | Problem(112) | Solution | Tag | Time | Space | Ref |
1153+
| Link | Problem(110) | Solution | Tag | Time | Space | Ref |
11501154
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
11511155
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
11521156
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -1173,7 +1177,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
11731177
| [Leetcode-237](https://leetcode.com/problems/delete-node-in-a-linked-list/) | Delete Node In A Linked List | [python3](./leetcode/237.delete-node-in-a-linked-list.py) | Other | \- | \- | - |
11741178
| [Leetcode-498](https://leetcode.com/problems/diagonal-traverse/) | Diagonal Traverse | [c++](./leetcode/498.diagonal-traverse.cpp) | Other | \- | \- | - |
11751179
| [Leetcode-29](https://leetcode.com/problems/divide-two-integers/) | Divide Two Integers | [c++](./leetcode/29.divide-two-integers.cpp), [python3](./leetcode/29.divide-two-integers.py) | Other | \- | \- | - |
1176-
| [Leetcode-282](https://leetcode.com/problems/expression-add-operators/) | Expression Add Operators | [c++](./leetcode/282.expression-add-operators.cpp), [python3](./leetcode/282.expression-add-operators.py) | Other | \- | \- | - |
11771180
| [Leetcode-760](https://leetcode.com/problems/find-anagram-mappings/) | Find Anagram Mappings | [python3](./leetcode/760.find-anagram-mappings.py) | Other | \- | \- | - |
11781181
| [Leetcode-652](https://leetcode.com/problems/find-duplicate-subtrees/) | Find Duplicate Subtrees | [c++](./leetcode/652.find-duplicate-subtrees.cpp) | Other | \- | \- | - |
11791182
| [Leetcode-366](https://leetcode.com/problems/find-leaves-of-binary-tree/) | Find Leaves Of Binary Tree | [c++](./leetcode/366.find-leaves-of-binary-tree.cpp), [python3](./leetcode/366.find-leaves-of-binary-tree.py) | Other | \- | \- | - |
@@ -1250,7 +1253,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
12501253
| [Leetcode-286](https://leetcode.com/problems/walls-and-gates/) | Walls And Gates | [c++](./leetcode/286.walls-and-gates.cpp), [python3](./leetcode/286.walls-and-gates.py) | Other | \- | \- | - |
12511254
| [Leetcode-127](https://leetcode.com/problems/word-ladder/) | Word Ladder | [c++](./leetcode/127.word-ladder.cpp), [python3](./leetcode/127.word-ladder.py) | Other | \- | \- | - |
12521255
| [Leetcode-6](https://leetcode.com/problems/zigzag-conversion/) | Zigzag Conversion | [python3](./leetcode/6.zigzag-conversion.py) | Other | \- | \- | - |
1253-
| [Lintcode-652](https://www.lintcode.com/problem/factorization/) | Factorization | [c++](./lintcode/652.factorization.cpp), [python3](./lintcode/652.factorization.py) | Other | \- | \- | - |
12541256
| [Lintcode-390](https://www.lintcode.com/problem/find-peak-element-ii/) | Find Peak Element II | [c++](./lintcode/390.find-peak-element-ii.cpp), [python3](./lintcode/390.find-peak-element-ii.py) | Other | \- | \- | - |
12551257
| [Lintcode-144](https://www.lintcode.com/problem/interleaving-positive-and-negative-numbers/) | Interleaving Positive And Negative Numbers | [c++](./lintcode/144.interleaving-positive-and-negative-numbers.cpp), [python3](./lintcode/144.interleaving-positive-and-negative-numbers.py) | Other | \- | \- | - |
12561258
| [Lintcode-611](https://www.lintcode.com/problem/knight-shortest-path/) | Knight Shortest Path | [c++](./lintcode/611.knight-shortest-path.cpp), [python3](./lintcode/611.knight-shortest-path.py) | Other | \- | \- | - |
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Tag: String, Dynamic Programming
2+
// Time: O(NM)
3+
// Space: O(NM)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/5fRF2U2DdEY
7+
8+
// Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.
9+
// A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: str1 = "abac", str2 = "cab"
14+
// Output: "cabac"
15+
// Explanation:
16+
// str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
17+
// str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
18+
// The answer provided is the shortest such string that satisfies these properties.
19+
//
20+
// Example 2:
21+
//
22+
// Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
23+
// Output: "aaaaaaaa"
24+
//
25+
//  
26+
// Constraints:
27+
//
28+
// 1 <= str1.length, str2.length <= 1000
29+
// str1 and str2 consist of lowercase English letters.
30+
//
31+
//
32+
33+
class Solution {
34+
public:
35+
string shortestCommonSupersequence(string str1, string str2) {
36+
int n = str1.size();
37+
int m = str2.size();
38+
39+
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
40+
41+
for (int i = 1; i <= n; i++) {
42+
for (int j = 1; j <= m; j++) {
43+
if (str1[i - 1] == str2[j - 1]) {
44+
dp[i][j] = dp[i - 1][j - 1] + 1;
45+
} else {
46+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
47+
}
48+
}
49+
}
50+
51+
vector<char> lcs(dp[n][m]);
52+
int i = n;
53+
int j = m;
54+
while (i > 0 && j > 0 && dp[i][j] > 0) {
55+
if (str1[i - 1] == str2[j - 1]) {
56+
lcs[dp[i][j] - 1] = str1[i - 1];
57+
i -= 1;
58+
j -= 1;
59+
} else if (dp[i - 1][j] > dp[i][j - 1]) {
60+
i -= 1;
61+
} else {
62+
j -= 1;
63+
}
64+
}
65+
66+
string res;
67+
i = 0;
68+
j = 0;
69+
for (auto &ch: lcs) {
70+
while (str1[i] != ch) {
71+
res += str1[i];
72+
i += 1;
73+
}
74+
75+
while (str2[j] != ch) {
76+
res += str2[j];
77+
j += 1;
78+
}
79+
80+
res += ch;
81+
i += 1;
82+
j += 1;
83+
}
84+
85+
if (i < n) {
86+
res += str1.substr(i);
87+
}
88+
89+
if (j < m) {
90+
res += str2.substr(j);
91+
}
92+
93+
return res;
94+
}
95+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Tag: String, Dynamic Programming
2+
# Time: O(NM)
3+
# Space: O(NM)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/5fRF2U2DdEY
7+
8+
# Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.
9+
# A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: str1 = "abac", str2 = "cab"
14+
# Output: "cabac"
15+
# Explanation:
16+
# str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
17+
# str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
18+
# The answer provided is the shortest such string that satisfies these properties.
19+
#
20+
# Example 2:
21+
#
22+
# Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
23+
# Output: "aaaaaaaa"
24+
#
25+
#  
26+
# Constraints:
27+
#
28+
# 1 <= str1.length, str2.length <= 1000
29+
# str1 and str2 consist of lowercase English letters.
30+
#
31+
#
32+
33+
class Solution:
34+
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
35+
n = len(str1)
36+
m = len(str2)
37+
38+
dp = [[0] * (m + 1) for i in range(n + 1)]
39+
for i in range(1, n + 1):
40+
for j in range(1, m + 1):
41+
if str1[i - 1] == str2[j - 1]:
42+
dp[i][j] = dp[i - 1][j - 1] + 1
43+
else:
44+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
45+
46+
i, j = n, m
47+
lcs = ['' for i in range(dp[n][m])]
48+
while i > 0 and j > 0 and dp[i][j] > 0:
49+
if str1[i - 1] == str2[j - 1]:
50+
lcs[dp[i][j] - 1] = str1[i - 1]
51+
i -= 1
52+
j -= 1
53+
elif dp[i - 1][j] > dp[i][j - 1]:
54+
i -= 1
55+
else:
56+
j -= 1
57+
58+
res = ''
59+
i = 0
60+
j = 0
61+
for k in lcs:
62+
while str1[i] != k:
63+
res += str1[i]
64+
i += 1
65+
66+
while str2[j] != k:
67+
res += str2[j]
68+
j += 1
69+
70+
res += k
71+
i += 1
72+
j += 1
73+
74+
if i < n:
75+
res += str1[i:]
76+
77+
if j < m:
78+
res += str2[j:]
79+
80+
return res

0 commit comments

Comments
 (0)