Skip to content

Commit

Permalink
241st Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 23, 2024
1 parent cafcf39 commit 7330056
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,14 @@ Must-do List for Interview Prep
| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium |
| 77. Combinations | Solution | Medium |
| 46. Permutations | [Solution][46] | Medium |
| 39. Combination Sum | Solution | Medium |
| 39. Combination Sum | [Solution][39] | Medium |
| 52. N-Queens II | Solution | Hard |
| 22. Generate Parentheses | Solution | Medium |
| 79. Word Search | Solution | Medium |

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

| Divide & Conquer | | |
| ----------------------------------------------- | --------------- | ------ |
Expand Down Expand Up @@ -587,14 +588,15 @@ 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 |
| 39. Combination Sum | [Solution][39] | 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
[39]: ./src/page-1/39.%20Combination%20Sum/combinationSum.ts
[46]: ./src/page-1/46.%20Permutations/permute.ts

| Binary Search | | |
Expand Down
8 changes: 4 additions & 4 deletions algorithms/13.backtracking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
```ts
const result = [];

function backtrack(state) {
function backtrack() {
// 紀錄解
if (x) {
push([...state]);
result.push();
return;
}

Expand All @@ -23,8 +23,8 @@ function backtrack(state) {
// 嘗試
push();

// 下一個
backtrack(state);
// 繼續探索下一個
backtrack();

// 回退
pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const letterCombinations: LetterCombinations = (digits) => {
result.push(combination);
} else {
const digit = nextDigits[0];

for (const letter of phone[digit]) {
backtrack(combination + letter, nextDigits.slice(1));
}
Expand Down
15 changes: 15 additions & 0 deletions src/page-1/39. Combination Sum/combinationSum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { combinationSum } from './combinationSum';

describe('39. Combination Sum', () => {
test('combinationSum', () => {
expect(combinationSum([2, 3, 6, 7], 7)).toStrictEqual([[2, 2, 3], [7]]);

expect(combinationSum([2, 3, 5], 8)).toStrictEqual([
[2, 2, 2, 2],
[2, 3, 3],
[3, 5],
]);

expect(combinationSum([2], 1)).toStrictEqual([]);
});
});
44 changes: 44 additions & 0 deletions src/page-1/39. Combination Sum/combinationSum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type CombinationSum = (candidates: number[], target: number) => number[][];

/**
* Accepted
*/
export const combinationSum: CombinationSum = (candidates, target) => {
// Result array to store all unique combinations
const result: number[][] = [];

// Helper function to perform the backtracking
// remaining: the remaining target to achieve
// combination: current combination being built
// index: current candidate index to explore
function backtrack(remaining: number, combination: number[], index: number): void {
// Base case: if remaining is 0, we found a valid combination
if (remaining === 0) {
// Push a copy of the current combination to the result array
result.push([...combination]);
return;
}

// Loop through the candidates starting from the current index
for (let i = index; i < candidates.length; i++) {
// If the candidate is greater than the remaining target, skip it
if (candidates[i] > remaining) continue;

// Add the current candidate to the combination
combination.push(candidates[i]);

// Recurse by reducing the remaining target by the current candidate's value
// and keeping the same index since we can use the same number multiple times
backtrack(remaining - candidates[i], combination, i);

// Backtrack by removing the last candidate added
combination.pop();
}
}

// Start the backtracking process with the initial target and an empty combination
backtrack(target, [], 0);

// Return the list of all valid combinations
return result;
};

0 comments on commit 7330056

Please sign in to comment.