-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0216-combination-sum-iii.js
More file actions
37 lines (33 loc) · 909 Bytes
/
0216-combination-sum-iii.js
File metadata and controls
37 lines (33 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 216. Combination Sum III
* https://leetcode.com/problems/combination-sum-iii/
* Difficulty: Medium
*
* Find all valid combinations of k numbers that sum up to n such that the following
* conditions are true:
* - Only numbers 1 through 9 are used.
* - Each number is used at most once.
*
* Return a list of all possible valid combinations. The list must not contain the same
* combination twice, and the combinations may be returned in any order.
*/
/**
* @param {number} k
* @param {number} n
* @return {number[][]}
*/
var combinationSum3 = function(k, n) {
const result = [];
function backtrack(history, sum, start) {
if (sum > n) return;
if (history.length === k && sum === n) {
result.push(history);
return;
}
for (let i = start; i < 10; i++) {
backtrack([...history, i], sum + i, i + 1);
}
}
backtrack([], 0, 1);
return result;
};