-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cafcf39
commit 7330056
Showing
5 changed files
with
68 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |