-
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
7fcf57f
commit cafcf39
Showing
8 changed files
with
222 additions
and
12 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
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
15 changes: 15 additions & 0 deletions
15
src/page-2/216. Combination Sum III/combinationSum3.test.ts
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 { combinationSum3 } from './combinationSum3'; | ||
|
||
describe('216. Combination Sum III', () => { | ||
test('combinationSum3', () => { | ||
expect(combinationSum3(3, 7)).toStrictEqual([[1, 2, 4]]); | ||
|
||
expect(combinationSum3(3, 9)).toStrictEqual([ | ||
[1, 2, 6], | ||
[1, 3, 5], | ||
[2, 3, 4], | ||
]); | ||
|
||
expect(combinationSum3(4, 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,28 @@ | ||
type CombinationSum3 = (k: number, n: number) => number[][]; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const combinationSum3: CombinationSum3 = (k, n) => { | ||
const result: number[][] = []; | ||
|
||
function backtrack(start: number, remaining: number, combination: number[]) { | ||
if (combination.length === k && remaining === 0) { | ||
result.push([...combination]); | ||
return; | ||
} | ||
|
||
if (combination.length > k || remaining < 0) return; | ||
|
||
// Try all numbers from 'start' to 9 | ||
for (let i = start; i <= 9; i++) { | ||
combination.push(i); | ||
backtrack(i + 1, remaining - i, combination); | ||
combination.pop(); | ||
} | ||
} | ||
|
||
backtrack(1, n, []); | ||
|
||
return result; | ||
}; |