Skip to content

Commit

Permalink
238th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 22, 2024
1 parent 9b164a1 commit 058850a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 69 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,17 @@ Must-do List for Interview Prep

[208]: ./src/page-2/208.%20Implement%20Trie%20(Prefix%20Tree)/Trie.ts

| Backtracking | | |
| ----------------------------------------- | -------- | ------ |
| 17. Letter Combinations of a Phone Number | Solution | Medium |
| 77. Combinations | Solution | Medium |
| 46. Permutations | Solution | Medium |
| 39. Combination Sum | Solution | Medium |
| 52. N-Queens II | Solution | Hard |
| 22. Generate Parentheses | Solution | Medium |
| 79. Word Search | Solution | Medium |
| Backtracking | | |
| ----------------------------------------- | -------------- | ------ |
| 17. Letter Combinations of a Phone Number | Solution | Medium |
| 77. Combinations | Solution | Medium |
| 46. Permutations | [Solution][46] | Medium |
| 39. Combination Sum | Solution | Medium |
| 52. N-Queens II | Solution | Hard |
| 22. Generate Parentheses | Solution | Medium |
| 79. Word Search | Solution | Medium |

[46]: ./src/page-1/46.%20Permutations/permute.ts

| Divide & Conquer | | |
| ----------------------------------------------- | --------------- | ------ |
Expand Down Expand Up @@ -584,13 +586,14 @@ Must-do List for Interview Prep
| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium |
| 22. Generate Parentheses | Solution | Medium |
| 39. Combination Sum | Solution | Medium |
| 46. Permutations | Solution | Medium |
| 46. Permutations | [Solution][46] | Medium |
| 51. N-Queens | Solution | Hard |
| 78. Subsets | Solution | Medium |
| 79. Word Search | Solution | Medium |
| 131. Palindrome Partitioning | Solution | Medium |

[17]: ./src/page-1/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/letterCombinations.ts
[46]: ./src/page-1/46.%20Permutations/permute.ts

| Binary Search | | |
| ----------------------------------------------------------- | -------------- | ------ |
Expand Down
63 changes: 4 additions & 59 deletions algorithms/13.backtracking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,65 +31,10 @@ flowchart TB
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);
const selected: boolean[] = Array(nums.length).fill(false);

function backtrack(currentPermutation: number[]) {
// 如果當前排列的長度等於原數組長度,說明我們已經生成了一個完整排列
Expand All @@ -101,18 +46,18 @@ function permute(nums: number[]): number[][] {
// 走訪可用數字
for (let i = 0; i < nums.length; i++) {
// 如果該數字已經被選過,則跳過 (剪枝)
if (used[i]) continue;
if (selected[i]) continue;

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

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

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

Expand Down
21 changes: 21 additions & 0 deletions src/page-1/46. Permutations/permute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { permute } from './permute';

describe('46. Permutations', () => {
test('permute', () => {
expect(permute([1, 2, 3])).toStrictEqual([
[1, 2, 3],
[1, 3, 2],
[2, 1, 3],
[2, 3, 1],
[3, 1, 2],
[3, 2, 1],
]);

expect(permute([0, 1])).toStrictEqual([
[0, 1],
[1, 0],
]);

expect(permute([1])).toStrictEqual([[1]]);
});
});
38 changes: 38 additions & 0 deletions src/page-1/46. Permutations/permute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
type Permute = (nums: number[]) => number[][];

/**
* Accepted
*/
export const permute: Permute = (nums) => {
const result: number[][] = [];
const selected: boolean[] = Array(nums.length).fill(false);

function backtrack(currentPermutation: number[]) {
// If the current permutation's length equals the input array's length, we have a complete permutation
if (currentPermutation.length === nums.length) {
result.push([...currentPermutation]);
return;
}

// Traverse the available numbers
for (let i = 0; i < nums.length; i++) {
// Skip if the number has already been selected (pruning)
if (selected[i]) continue;

// Make a choice
currentPermutation.push(nums[i]);
selected[i] = true;

// Recursively make the next choice
backtrack(currentPermutation);

// Undo the choice (backtrack)
currentPermutation.pop();
selected[i] = false;
}
}

backtrack([]); // Start backtracking

return result;
};

0 comments on commit 058850a

Please sign in to comment.