forked from soulmachine/algorithm-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination-sum-iii.java
More file actions
29 lines (26 loc) · 908 Bytes
/
Copy pathcombination-sum-iii.java
File metadata and controls
29 lines (26 loc) · 908 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
// Combination Sum III
// Time Complexity: O(9*8*...*(10-k)), Space Complexity: O(k)
public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
final List<List<Integer>> result = new ArrayList<>();
final List<Integer> path = new ArrayList<>();
dfs(k, n, path, result);
return result;
}
private static void dfs(int step, int gap, List<Integer> path,
List<List<Integer>> result) {
if (step == 0) {
if (gap == 0) {
result.add(new ArrayList<>(path));
}
return;
}
if (gap < 1) return;
final int start = path.isEmpty() ? 1 : path.get(path.size() - 1)+1;
for (int i = start; i < 10; ++i) {
path.add(i);
dfs(step - 1, gap - i, path, result);
path.remove(path.size() - 1);
}
}
}