Skip to content

Commit

Permalink
237th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 22, 2024
1 parent 21df0d3 commit 9b164a1
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ $ pnpm test twoSum.test.ts
9. [字典樹 (Trie)](./algorithms/9.trie/README.md)
10. [排序 (Sorting)](./algorithms/10.sorting/README.md)
11. [搜尋 (Searching)](./algorithms/11.searching/README.md)
12. 分治 (Divide and Conquer)
13. 回溯 (Backtracking)
12. [分治 (Divide and Conquer)](./algorithms/12.divide-and-conquer/README.md)
13. [回溯 (Backtracking)](./algorithms/13.backtracking/README.md)
14. [動態規劃 (Dynamic Programming)](./algorithms/14.dynamic-programming/README.md)
15. 貪婪 (Greedy)
16. 位元操作 (Bit Manipulation)
Expand Down
47 changes: 47 additions & 0 deletions algorithms/12.divide-and-conquer/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
# 分治 (Divide and Conquer)

- 分 (劃分)
- 治 (合併)

```ts
[8, 4, 2, 6];
```

分:

```mermaid
flowchart TB
0(8, 4, 2, 6)
1(8, 4)
2(2, 6)
3(8)
4(4)
5(2)
6(6)
0 --> 1
0 --> 2
1 --> 3
1 --> 4
2 --> 5
2 --> 6
```

治:

```mermaid
flowchart TB
0(8)
1(4)
2(2)
3(6)
4(4, 8)
5(2, 6)
6(2, 4, 6, 8)
0 --> 4
1 --> 4
2 --> 5
3 --> 5
4 --> 6
5 --> 6
```
122 changes: 122 additions & 0 deletions algorithms/13.backtracking/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,123 @@
# 回溯 (Backtracking)

### 全排列 (Permutations)

```ts
[1, 2];
```

```ts
[
[1, 2],
[2, 1],
];
```

```mermaid
flowchart TB
0(_, _)
1(1, _)
2(2, _)
3(1, 1)
4(1, 2)
5(2, 1)
6(2, 2)
0 --> 1
0 --> 2
1 -. 剪枝 .-> 3
1 --> 4
2 --> 5
2 -. 剪枝 .-> 6
```

```ts
[1, 2, 3];
```

```ts
[
[1, 2, 3],
[1, 3, 2],
[2, 1, 3],
[2, 3, 1],
[3, 1, 2],
[3, 2, 1],
];
```

```mermaid
flowchart TB
0(_, _, _)
1(1, _, _)
2(2, _, _)
3(3, _, _)
4(1, 2, _)
5(1, 3, _)
6(2, 1, _)
7(2, 3, _)
8(3, 1, _)
9(3, 2, _)
10(1, 2, 3)
11(1, 3, 2)
12(2, 1, 3)
13(2, 3, 1)
14(3, 1, 2)
15(3, 2, 1)
0 --> 1
0 --> 2
0 --> 3
1 --> 4
1 --> 5
2 --> 6
2 --> 7
3 --> 8
3 --> 9
4 --> 10
5 --> 11
6 --> 12
7 --> 13
8 --> 14
9 --> 15
```

```ts
function permute(nums: number[]): number[][] {
const result: number[][] = [];
const used: boolean[] = Array(nums.length).fill(false);

function backtrack(currentPermutation: number[]) {
// 如果當前排列的長度等於原數組長度,說明我們已經生成了一個完整排列
if (currentPermutation.length === nums.length) {
result.push([...currentPermutation]);
return;
}

// 走訪可用數字
for (let i = 0; i < nums.length; i++) {
// 如果該數字已經被選過,則跳過 (剪枝)
if (used[i]) continue;

// 做選擇
currentPermutation.push(nums[i]);
used[i] = true;

// 遞迴進行下一步選擇
backtrack(currentPermutation);

// 撤銷選擇 (回溯)
currentPermutation.pop();
used[i] = false;
}
}

backtrack([]); // 開始回溯

return result;
}
```
28 changes: 14 additions & 14 deletions algorithms/7.heap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import { BinaryHeap, ascend, descend } from '@std/data-structures';
```

```mermaid
graph TD
0[1] --> 1[3]
0 --> 2[6]
1 --> 3[5]
1 --> 4[9]
2 --> 5[8]
2 --> 6[15]
flowchart TB
0(1) --> 1(3)
0 --> 2(6)
1 --> 3(5)
1 --> 4(9)
2 --> 5(8)
2 --> 6(15)
```

```ts
Expand Down Expand Up @@ -56,13 +56,13 @@ console.log(minHeap.isEmpty()); // false
```

```mermaid
graph TD
0[15] --> 1[9]
0 --> 2[8]
1 --> 3[1]
1 --> 4[3]
2 --> 5[6]
2 --> 6[5]
flowchart TB
0(15) --> 1(9)
0 --> 2(8)
1 --> 3(1)
1 --> 4(3)
2 --> 5(6)
2 --> 6(5)
```

```ts
Expand Down

0 comments on commit 9b164a1

Please sign in to comment.