Skip to content

Commit bebcb52

Browse files
committed
feat: add solutions to lc problems: No.0072,1143
* No.0072.Edit Distance * No.1143.Longest Common Subsequence
1 parent 60579ab commit bebcb52

File tree

7 files changed

+186
-6
lines changed

7 files changed

+186
-6
lines changed

solution/0000-0099/0072.Edit Distance/README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ exection -> execution (插入 'u')
5757

5858
**方法一:动态规划**
5959

60-
我们定义 $f[i][j]$ 表示将 $word1$ 的前 $i$ 个字符转换成 $word2$ 的前 $j$ 个字符所使用的最少操作数。初始时 $f[i][0] = i$, f[0][j] = j$。其中 $i \in [1, m], j \in [0, n]$。
60+
我们定义 $f[i][j]$ 表示将 $word1$ 的前 $i$ 个字符转换成 $word2$ 的前 $j$ 个字符所使用的最少操作数。初始时 $f[i][0] = i$, $f[0][j] = j$。其中 $i \in [1, m], j \in [0, n]$。
6161

6262
考虑 $f[i][j]$:
6363

@@ -215,6 +215,38 @@ function minDistance(word1: string, word2: string): number {
215215
}
216216
```
217217

218+
### **JavaScript**
219+
220+
```js
221+
/**
222+
* @param {string} word1
223+
* @param {string} word2
224+
* @return {number}
225+
*/
226+
var minDistance = function (word1, word2) {
227+
const m = word1.length;
228+
const n = word2.length;
229+
const f = Array(m + 1)
230+
.fill(0)
231+
.map(() => Array(n + 1).fill(0));
232+
for (let j = 1; j <= n; ++j) {
233+
f[0][j] = j;
234+
}
235+
for (let i = 1; i <= m; ++i) {
236+
f[i][0] = i;
237+
for (let j = 1; j <= n; ++j) {
238+
if (word1[i - 1] === word2[j - 1]) {
239+
f[i][j] = f[i - 1][j - 1];
240+
} else {
241+
f[i][j] =
242+
Math.min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1;
243+
}
244+
}
245+
}
246+
return f[m][n];
247+
};
248+
```
249+
218250
### **...**
219251

220252
```

solution/0000-0099/0072.Edit Distance/README_EN.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,29 @@ exection -&gt; execution (insert &#39;u&#39;)
4949

5050
## Solutions
5151

52-
Dynamic programming.
52+
**Solution 1: Dynamic Programming**
53+
54+
We define $f[i][j]$ as the minimum number of operations to convert $word1$ of length $i$ to $word2$ of length $j$. $f[i][0] = i$, $f[0][j] = j$, $i \in [1, m], j \in [0, n]$.
55+
56+
We consider $f[i][j]$:
57+
58+
- If $word1[i - 1] = word2[j - 1]$, then we only need to consider the minimum number of operations to convert $word1$ of length $i - 1$ to $word2$ of length $j - 1$, so $f[i][j] = f[i - 1][j - 1]$;
59+
- Otherwise, we can consider insert, delete, and replace operations, then $f[i][j] = \min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1$.
60+
61+
Finally, we can get the state transition equation:
62+
63+
$$
64+
f[i][j] = \begin{cases}
65+
i, & \text{if } j = 0 \\
66+
j, & \text{if } i = 0 \\
67+
f[i - 1][j - 1], & \text{if } word1[i - 1] = word2[j - 1] \\
68+
\min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1, & \text{otherwise}
69+
\end{cases}
70+
$$
71+
72+
Finally, we return $f[m][n]$.
73+
74+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. $m$ and $n$ are the lengths of $word1$ and $word2$ respectively.
5375

5476
<!-- tabs:start -->
5577

@@ -183,6 +205,38 @@ function minDistance(word1: string, word2: string): number {
183205
}
184206
```
185207

208+
### **JavaScript**
209+
210+
```js
211+
/**
212+
* @param {string} word1
213+
* @param {string} word2
214+
* @return {number}
215+
*/
216+
var minDistance = function (word1, word2) {
217+
const m = word1.length;
218+
const n = word2.length;
219+
const f = Array(m + 1)
220+
.fill(0)
221+
.map(() => Array(n + 1).fill(0));
222+
for (let j = 1; j <= n; ++j) {
223+
f[0][j] = j;
224+
}
225+
for (let i = 1; i <= m; ++i) {
226+
f[i][0] = i;
227+
for (let j = 1; j <= n; ++j) {
228+
if (word1[i - 1] === word2[j - 1]) {
229+
f[i][j] = f[i - 1][j - 1];
230+
} else {
231+
f[i][j] =
232+
Math.min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1;
233+
}
234+
}
235+
}
236+
return f[m][n];
237+
};
238+
```
239+
186240
### **...**
187241

188242
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {number}
5+
*/
6+
var minDistance = function (word1, word2) {
7+
const m = word1.length;
8+
const n = word2.length;
9+
const f = Array(m + 1)
10+
.fill(0)
11+
.map(() => Array(n + 1).fill(0));
12+
for (let j = 1; j <= n; ++j) {
13+
f[0][j] = j;
14+
}
15+
for (let i = 1; i <= m; ++i) {
16+
f[i][0] = i;
17+
for (let j = 1; j <= n; ++j) {
18+
if (word1[i - 1] === word2[j - 1]) {
19+
f[i][j] = f[i - 1][j - 1];
20+
} else {
21+
f[i][j] =
22+
Math.min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1;
23+
}
24+
}
25+
}
26+
return f[m][n];
27+
};

solution/1100-1199/1143.Longest Common Subsequence/README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $$
6565
f[i][j] =
6666
\begin{cases}
6767
f[i - 1][j - 1] + 1, & text1[i - 1] = text2[j - 1] \\
68-
max(f[i - 1][j], f[i][j - 1]), & text1[i - 1] \neq text2[j - 1]
68+
\max(f[i - 1][j], f[i][j - 1]), & text1[i - 1] \neq text2[j - 1]
6969
\end{cases}
7070
$$
7171

@@ -254,6 +254,28 @@ public class Solution {
254254
}
255255
```
256256

257+
### **Kotlin**
258+
259+
```kotlin
260+
class Solution {
261+
fun longestCommonSubsequence(text1: String, text2: String): Int {
262+
val m = text1.length
263+
val n = text2.length
264+
val f = Array(m + 1) { IntArray(n + 1) }
265+
for (i in 1..m) {
266+
for (j in 1..n) {
267+
if (text1[i - 1] == text2[j - 1]) {
268+
f[i][j] = f[i - 1][j - 1] + 1
269+
} else {
270+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1])
271+
}
272+
}
273+
}
274+
return f[m][n]
275+
}
276+
}
277+
```
278+
257279
### **...**
258280

259281
```

solution/1100-1199/1143.Longest Common Subsequence/README_EN.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,22 @@
4949

5050
## Solutions
5151

52-
Dynamic programming.
52+
**Solution 1: Dynamic Programming**
53+
54+
Let $f[i][j]$ be the length of the longest common subsequence of $text1$ and $text2$ with length $i$ and $j$, respectively. The answer is $f[m][n]$, where $m$ and $n$ are the lengths of $text1$ and $text2$, respectively.
55+
56+
If the $i$-th character of $text1$ and the $j$-th character of $text2$ are the same, then $f[i][j] = f[i - 1][j - 1] + 1$; if the $i$-th character of $text1$ and the $j$-th character of $text2$ are different, then $f[i][j] = max(f[i - 1][j], f[i][j - 1])$. That is, the state transition equation is:
5357

5458
$$
5559
f[i][j] =
5660
\begin{cases}
5761
f[i - 1][j - 1] + 1, & text1[i - 1] = text2[j - 1] \\
58-
max(f[i - 1][j], f[i][j - 1]), & text1[i - 1] \neq text2[j - 1]
62+
\max(f[i - 1][j], f[i][j - 1]), & text1[i - 1] \neq text2[j - 1]
5963
\end{cases}
6064
$$
6165

66+
Time complexity $O(m \times n)$, space complexity $O(m \times n)$. Where $m$ and $n$ are the lengths of $text1$ and $text2$, respectively.
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
@@ -238,6 +244,28 @@ public class Solution {
238244
}
239245
```
240246

247+
### **Kotlin**
248+
249+
```kotlin
250+
class Solution {
251+
fun longestCommonSubsequence(text1: String, text2: String): Int {
252+
val m = text1.length
253+
val n = text2.length
254+
val f = Array(m + 1) { IntArray(n + 1) }
255+
for (i in 1..m) {
256+
for (j in 1..n) {
257+
if (text1[i - 1] == text2[j - 1]) {
258+
f[i][j] = f[i - 1][j - 1] + 1
259+
} else {
260+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1])
261+
}
262+
}
263+
}
264+
return f[m][n]
265+
}
266+
}
267+
```
268+
241269
### **...**
242270

243271
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
fun longestCommonSubsequence(text1: String, text2: String): Int {
3+
val m = text1.length
4+
val n = text2.length
5+
val f = Array(m + 1) { IntArray(n + 1) }
6+
for (i in 1..m) {
7+
for (j in 1..n) {
8+
if (text1[i - 1] == text2[j - 1]) {
9+
f[i][j] = f[i - 1][j - 1] + 1
10+
} else {
11+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1])
12+
}
13+
}
14+
}
15+
return f[m][n]
16+
}
17+
}

solution/1100-1199/1144.Decrease Elements To Make Array Zigzag/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
我们可以分别枚举偶数位和奇数位作为“比相邻元素小”的元素,然后计算需要的操作次数。取两者的最小值即可。
5151

52-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
52+
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$
5353

5454
<!-- tabs:start -->
5555

0 commit comments

Comments
 (0)