Skip to content

Commit

Permalink
259th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Dec 30, 2024
1 parent 49b570f commit 5de5051
Show file tree
Hide file tree
Showing 6 changed files with 519 additions and 337 deletions.
674 changes: 340 additions & 334 deletions README.md

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions src/page-1/72. Edit Distance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# 72. 編輯距離 (Edit Distance)

給定兩個字串 word1 和 word2,返回將 word1 轉換為 word2 所使用的最少操作數。

你可以對字詞進行以下三種操作:

- 插入一個字元
- 刪除一個字元
- 替換一個字元

範例 1:

```coffee
輸入: word1 = "horse", word2 = "ros"
輸出: 3
說明:
horse -> rorse (將 'h' 替換為 'r')
rorse -> rose (刪除 'r')
rose -> ros (刪除 'e')
```

範例 2:

```coffee
輸入: word1 = "intention", word2 = "execution"
輸出: 5
說明:
intention -> inention (刪除 't')
inention -> enention (將 'i' 替換為 'e')
enention -> exention (將 'n' 替換為 'x')
exention -> exection (將 'n' 替換為 'c')
exection -> execution (插入 'u')
```

## 解題

- 插入操作:`dp[i][j-1] + 1`

```coffee
word1: "abc" -> "abcd"
word2: "abcd"
```

| | "" | a | b | c | d |
| --- | --- | --- | --- | --- | --- |
| "" | 0 | 1 | 2 | 3 | 4 |
| a | 1 | 0 | 1 | 2 | 3 |
| ab | 2 | 1 | 0 | 1 | 2 |
| abc | 3 | 2 | 1 | 0 | 1 |

- 刪除操作:`dp[i-1][j] + 1`

```coffee
word1: "abc" -> "ab"
word2: "ab"
```

| | "" | a | b | c |
| --- | --- | --- | --- | --- |
| "" | 0 | 1 | 2 | 3 |
| a | 1 | 0 | 1 | 2 |
| ab | 2 | 1 | 0 | 1 |
| abc | 3 | 2 | 1 | 1 |

- 替換操作:`dp[i-1][j-1] + 1`

```coffee
word1: "abc" -> "abd"
word2: "abd"
```

| | "" | a | b | c |
| --- | --- | --- | --- | --- |
| "" | 0 | 1 | 2 | 3 |
| a | 1 | 0 | 1 | 2 |
| ab | 2 | 1 | 0 | 1 |
| abc | 3 | 2 | 1 | 1 |
| adc | 3 | 2 | 1 | 1 |

```ts
export const minDistance: MinDistance = (word1, word2) => {
const m = word1.length;
const n = word2.length;

// 建立一個二維 DP 表
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));

// 空字串情況處理
for (let i = 0; i <= m; i++) dp[i][0] = i; // 刪除 word1 中的所有字元
for (let j = 0; j <= n; j++) dp[0][j] = j; // 插入所有字元以匹配 word2

// 填充 DP 表
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (word1[i - 1] === word2[j - 1]) {
// 字元匹配,無需操作
dp[i][j] = dp[i - 1][j - 1];
} else {
// 選擇最少的插入、刪除或替換操作
dp[i][j] = Math.min(
dp[i][j - 1] + 1, // 插入
dp[i - 1][j] + 1, // 刪除
dp[i - 1][j - 1] + 1, // 替換
);
}
}
}

// 結果位於右下角格子中
return dp[m][n];
};
```
8 changes: 8 additions & 0 deletions src/page-1/72. Edit Distance/minDistance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { minDistance } from './minDistance';

describe('72. Edit Distance', () => {
test('minDistance', () => {
expect(minDistance('horse', 'ros')).toBe(3);
expect(minDistance('intention', 'execution')).toBe(5);
});
});
36 changes: 36 additions & 0 deletions src/page-1/72. Edit Distance/minDistance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type MinDistance = (word1: string, word2: string) => number;

/**
* Accepted
*/
export const minDistance: MinDistance = (word1, word2) => {
const m = word1.length;
const n = word2.length;

// Create a 2D DP array
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));

// If one of the strings is empty, we need to delete or insert all characters of the other string
for (let i = 0; i <= m; i++) dp[i][0] = i; // Deleting all characters from word1
for (let j = 0; j <= n; j++) dp[0][j] = j; // Inserting all characters to match word2

// Fill the DP table
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (word1[i - 1] === word2[j - 1]) {
// Characters match, no operation needed
dp[i][j] = dp[i - 1][j - 1];
} else {
// Choose the minimum of insert, delete, or replace operation
dp[i][j] = Math.min(
dp[i][j - 1] + 1, // Insert
dp[i - 1][j] + 1, // Delete
dp[i - 1][j - 1] + 1, // Replace
);
}
}
}

// The result is in the bottom-right cell
return dp[m][n];
};
6 changes: 3 additions & 3 deletions src/page-17/1768. Merge Strings Alternately/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

範例 1:

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

範例 2:

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

範例 3:

```ts
```coffee
輸入: word1 = "abcd", word2 = "pq"
輸出: "apbqcd"
說明: 注意,word1 比 word2 長,"cd" 需要追加到合併後字串的結尾。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@
- 你不得同時進行多項交易 (即,您必須在再次購買之前出售股票)。
- 每次買賣股票只收取一次手續費。

範例 1:

```coffee
輸入: prices = [1,3,2,8,4,9], fee = 2
輸出: 8
說明: 可以透過以下方式實現最大利潤:
- 以 prices[0] = 1 買入
- 以 prices[3] = 8 賣出
- 以 prices[4] = 4 買入
- 以 prices[5] = 9 賣出
總利潤為 ((8 - 1) - 2) + ((9 - 4) - 2) = 8
```

範例 2:

```coffee
輸入: prices = [1,3,7,5,10,3], fee = 3
輸出: 6
```

## 解題

使用動態規劃 (Dynamic Programming)
Expand Down

0 comments on commit 5de5051

Please sign in to comment.