Skip to content

Commit 3ee4d9f

Browse files
committed
update 72
1 parent 8921868 commit 3ee4d9f

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This is an open-source project that is continually updated
1616

1717
**Total:** 433 problems
1818

19-
**Updated:** 2024-09-10 15:17:59
19+
**Updated:** 2024-09-10 18:44:26
2020

2121
## 链接/Links
2222

@@ -214,7 +214,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
214214
| 2024-07 | [Leetcode-416. Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) | [c++](./leetcode/416.partition-equal-subset-sum.cpp), [python3](./leetcode/416.partition-equal-subset-sum.py) | Dynamic Programming | O\(N^2\) | O\(N\) | - |
215215
| 2024-07 | [Leetcode-474. Ones And Zeroes](https://leetcode.com/problems/ones-and-zeroes/description/) | [c++](./leetcode/474.ones-and-zeroes.cpp), [python3](./leetcode/474.ones-and-zeroes.py) | Dynamic Programming | O\(KMN\) | O\(MN\) | - |
216216
| 2024-07 | [Leetcode-322. Coin Change](https://leetcode.com/problems/coin-change/description/) | [c++](./leetcode/322.coin-change.cpp), [python3](./leetcode/322.coin-change.py) | Dynamic Programming | O\(K \* N\) | O\(N\) | [Video](https://youtu.be/EjMjlFjLRiM) |
217-
| 2024-07 | [Leetcode-72. Edit Distance](https://leetcode.com/problems/edit-distance/description/) | [c++](./leetcode/72.edit-distance.cpp), [python3](./leetcode/72.edit-distance.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
218217
| 2024-07 | [Leetcode-650. 2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/description/) | [c++](./leetcode/650.2-keys-keyboard.cpp), [python3](./leetcode/650.2-keys-keyboard.py) | Dynamic Programming | \- | \- | - |
219218
| 2024-07 | [Leetcode-10. Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/description/) | [c++](./leetcode/10.regular-expression-matching.cpp), [python3](./leetcode/10.regular-expression-matching.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
220219
| 2024-07 | [Leetcode-213. House Robber II](https://leetcode.com/problems/house-robber-ii/description/) | [c++](./leetcode/213.house-robber-ii.cpp), [python3](./leetcode/213.house-robber-ii.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
@@ -243,6 +242,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
243242
| 2024-09 | [Leetcode-188. Best Time To Buy And Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | [c++](./leetcode/188.best-time-to-buy-and-sell-stock-iv.cpp), [python3](./leetcode/188.best-time-to-buy-and-sell-stock-iv.py) | Dynamic Programming | O\(NK\) | O\(NK\) | - |
244243
| 2024-09 | [Leetcode-714. Best Time To Buy And Sell Stock With Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) | [c++](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp), [python3](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
245244
| 2024-09 | [Leetcode-309. Best Time To Buy And Sell Stock With Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) | [c++](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.cpp), [python3](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
245+
| 2024-09 | [Leetcode-72. Edit Distance](https://leetcode.com/problems/edit-distance/description/) | [c++](./leetcode/72.edit-distance.cpp), [python3](./leetcode/72.edit-distance.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
246246

247247
## Binary Search
248248

leetcode/72.edit-distance.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,40 @@ class Solution {
6565
return dp[m][n];
6666

6767
}
68+
};
69+
70+
class Solution {
71+
public:
72+
int minDistance(string word1, string word2) {
73+
int n = word1.size();
74+
int m = word2.size();
75+
vector<vector<int>> cache(n, vector<int>(m, INT_MAX));
76+
return helper(word1, word2, 0, 0, cache);
77+
}
78+
79+
int helper(string &word1, string &word2, int i, int j, vector<vector<int>> &cache) {
80+
if (i == word1.size()) {
81+
return word2.size() - j;
82+
}
83+
84+
if (j == word2.size()) {
85+
return word1.size() - i;
86+
}
87+
88+
if (cache[i][j] != INT_MAX) {
89+
return cache[i][j];
90+
}
91+
92+
if (word1[i] == word2[j]) {
93+
cache[i][j] = helper(word1, word2, i + 1, j + 1, cache);
94+
} else {
95+
int insert = helper(word1, word2, i, j + 1, cache) + 1;
96+
int remove = helper(word1, word2, i + 1, j, cache) + 1;
97+
int replace = helper(word1, word2, i + 1, j + 1, cache) + 1;
98+
99+
cache[i][j] = min(min(insert, remove), replace);
100+
}
101+
102+
return cache[i][j];
103+
}
68104
};

leetcode/72.edit-distance.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,32 @@ def minDistance(self, word1: str, word2: str) -> int:
5757
else:
5858
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
5959

60-
return dp[m][n]
60+
return dp[m][n]
61+
62+
63+
class Solution:
64+
def minDistance(self, word1: str, word2: str) -> int:
65+
n = len(word1)
66+
m = len(word2)
67+
cache = [[None] * m for i in range(n)]
68+
return self.helper(word1, word2, 0, 0, cache) # dict cache is too slow
69+
70+
def helper(self, word1: str, word2: str, i :int, j: int, cache: dict) -> int:
71+
if i == len(word1):
72+
return len(word2) - j
73+
74+
if j == len(word2):
75+
return len(word1) - i
76+
77+
if cache[i][j] is not None:
78+
return cache[i][j]
79+
80+
if word1[i] == word2[j]:
81+
cache[i][j] = self.helper(word1, word2, i + 1, j + 1, cache)
82+
else:
83+
insert = self.helper(word1, word2, i, j + 1, cache) + 1
84+
delete = self.helper(word1, word2, i + 1, j, cache) + 1
85+
replace = self.helper(word1, word2, i + 1, j + 1, cache) + 1
86+
cache[i][j] = min(insert, delete, replace)
87+
88+
return cache[i][j]

0 commit comments

Comments
 (0)