Skip to content

Commit 5de5051

Browse files
committed
259th Commit
1 parent 49b570f commit 5de5051

File tree

6 files changed

+519
-337
lines changed

6 files changed

+519
-337
lines changed

README.md

Lines changed: 340 additions & 334 deletions
Large diffs are not rendered by default.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# 72. 編輯距離 (Edit Distance)
2+
3+
給定兩個字串 word1 和 word2,返回將 word1 轉換為 word2 所使用的最少操作數。
4+
5+
你可以對字詞進行以下三種操作:
6+
7+
- 插入一個字元
8+
- 刪除一個字元
9+
- 替換一個字元
10+
11+
範例 1:
12+
13+
```coffee
14+
輸入: word1 = "horse", word2 = "ros"
15+
輸出: 3
16+
說明:
17+
horse -> rorse (將 'h' 替換為 'r')
18+
rorse -> rose (刪除 'r')
19+
rose -> ros (刪除 'e')
20+
```
21+
22+
範例 2:
23+
24+
```coffee
25+
輸入: word1 = "intention", word2 = "execution"
26+
輸出: 5
27+
說明:
28+
intention -> inention (刪除 't')
29+
inention -> enention (將 'i' 替換為 'e')
30+
enention -> exention (將 'n' 替換為 'x')
31+
exention -> exection (將 'n' 替換為 'c')
32+
exection -> execution (插入 'u')
33+
```
34+
35+
## 解題
36+
37+
- 插入操作:`dp[i][j-1] + 1`
38+
39+
```coffee
40+
word1: "abc" -> "abcd"
41+
word2: "abcd"
42+
```
43+
44+
| | "" | a | b | c | d |
45+
| --- | --- | --- | --- | --- | --- |
46+
| "" | 0 | 1 | 2 | 3 | 4 |
47+
| a | 1 | 0 | 1 | 2 | 3 |
48+
| ab | 2 | 1 | 0 | 1 | 2 |
49+
| abc | 3 | 2 | 1 | 0 | 1 |
50+
51+
- 刪除操作:`dp[i-1][j] + 1`
52+
53+
```coffee
54+
word1: "abc" -> "ab"
55+
word2: "ab"
56+
```
57+
58+
| | "" | a | b | c |
59+
| --- | --- | --- | --- | --- |
60+
| "" | 0 | 1 | 2 | 3 |
61+
| a | 1 | 0 | 1 | 2 |
62+
| ab | 2 | 1 | 0 | 1 |
63+
| abc | 3 | 2 | 1 | 1 |
64+
65+
- 替換操作:`dp[i-1][j-1] + 1`
66+
67+
```coffee
68+
word1: "abc" -> "abd"
69+
word2: "abd"
70+
```
71+
72+
| | "" | a | b | c |
73+
| --- | --- | --- | --- | --- |
74+
| "" | 0 | 1 | 2 | 3 |
75+
| a | 1 | 0 | 1 | 2 |
76+
| ab | 2 | 1 | 0 | 1 |
77+
| abc | 3 | 2 | 1 | 1 |
78+
| adc | 3 | 2 | 1 | 1 |
79+
80+
```ts
81+
export const minDistance: MinDistance = (word1, word2) => {
82+
const m = word1.length;
83+
const n = word2.length;
84+
85+
// 建立一個二維 DP 表
86+
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
87+
88+
// 空字串情況處理
89+
for (let i = 0; i <= m; i++) dp[i][0] = i; // 刪除 word1 中的所有字元
90+
for (let j = 0; j <= n; j++) dp[0][j] = j; // 插入所有字元以匹配 word2
91+
92+
// 填充 DP 表
93+
for (let i = 1; i <= m; i++) {
94+
for (let j = 1; j <= n; j++) {
95+
if (word1[i - 1] === word2[j - 1]) {
96+
// 字元匹配,無需操作
97+
dp[i][j] = dp[i - 1][j - 1];
98+
} else {
99+
// 選擇最少的插入、刪除或替換操作
100+
dp[i][j] = Math.min(
101+
dp[i][j - 1] + 1, // 插入
102+
dp[i - 1][j] + 1, // 刪除
103+
dp[i - 1][j - 1] + 1, // 替換
104+
);
105+
}
106+
}
107+
}
108+
109+
// 結果位於右下角格子中
110+
return dp[m][n];
111+
};
112+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { minDistance } from './minDistance';
2+
3+
describe('72. Edit Distance', () => {
4+
test('minDistance', () => {
5+
expect(minDistance('horse', 'ros')).toBe(3);
6+
expect(minDistance('intention', 'execution')).toBe(5);
7+
});
8+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
type MinDistance = (word1: string, word2: string) => number;
2+
3+
/**
4+
* Accepted
5+
*/
6+
export const minDistance: MinDistance = (word1, word2) => {
7+
const m = word1.length;
8+
const n = word2.length;
9+
10+
// Create a 2D DP array
11+
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
12+
13+
// If one of the strings is empty, we need to delete or insert all characters of the other string
14+
for (let i = 0; i <= m; i++) dp[i][0] = i; // Deleting all characters from word1
15+
for (let j = 0; j <= n; j++) dp[0][j] = j; // Inserting all characters to match word2
16+
17+
// Fill the DP table
18+
for (let i = 1; i <= m; i++) {
19+
for (let j = 1; j <= n; j++) {
20+
if (word1[i - 1] === word2[j - 1]) {
21+
// Characters match, no operation needed
22+
dp[i][j] = dp[i - 1][j - 1];
23+
} else {
24+
// Choose the minimum of insert, delete, or replace operation
25+
dp[i][j] = Math.min(
26+
dp[i][j - 1] + 1, // Insert
27+
dp[i - 1][j] + 1, // Delete
28+
dp[i - 1][j - 1] + 1, // Replace
29+
);
30+
}
31+
}
32+
}
33+
34+
// The result is in the bottom-right cell
35+
return dp[m][n];
36+
};

src/page-17/1768. Merge Strings Alternately/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
範例 1:
88

9-
```ts
9+
```coffee
1010
輸入: word1 = "abc", word2 = "pqr"
1111
輸出: "apbqcr"
1212
說明: 字串合併情況如下所示:
@@ -17,7 +17,7 @@ word2: p q r
1717

1818
範例 2:
1919

20-
```ts
20+
```coffee
2121
輸入: word1 = "ab", word2 = "pqrs"
2222
輸出: "apbqrs"
2323
說明: 注意,word2 比 word1 長,"rs" 需要追加到合併後字串的結尾。
@@ -28,7 +28,7 @@ word2: p q r s
2828

2929
範例 3:
3030

31-
```ts
31+
```coffee
3232
輸入: word1 = "abcd", word2 = "pq"
3333
輸出: "apbqcd"
3434
說明: 注意,word1 比 word2 長,"cd" 需要追加到合併後字串的結尾。

src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99
- 你不得同時進行多項交易 (即,您必須在再次購買之前出售股票)。
1010
- 每次買賣股票只收取一次手續費。
1111

12+
範例 1:
13+
14+
```coffee
15+
輸入: prices = [1,3,2,8,4,9], fee = 2
16+
輸出: 8
17+
說明: 可以透過以下方式實現最大利潤:
18+
- 以 prices[0] = 1 買入
19+
- 以 prices[3] = 8 賣出
20+
- 以 prices[4] = 4 買入
21+
- 以 prices[5] = 9 賣出
22+
總利潤為 ((8 - 1) - 2) + ((9 - 4) - 2) = 8
23+
```
24+
25+
範例 2:
26+
27+
```coffee
28+
輸入: prices = [1,3,7,5,10,3], fee = 3
29+
輸出: 6
30+
```
31+
1232
## 解題
1333

1434
使用動態規劃 (Dynamic Programming)

0 commit comments

Comments
 (0)