Skip to content

Commit 4bf41fd

Browse files
authored
Create yeonguchoe.java
1 parent c059cdd commit 4bf41fd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

combination-sum/yeonguchoe.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
// Time complexity: O(candidate^(target/smallest candidate))
3+
// Space complexity: O(target/smallest candidate)
4+
5+
List<List<Integer>> result = new ArrayList<>();
6+
7+
public void backtracking(int remainingAmount, List<Integer> combination, int startPoint, int[] candidateList,
8+
List<List<Integer>> resultList) {
9+
if (remainingAmount == 0) {
10+
resultList.add(new ArrayList<>(combination));
11+
return;
12+
}
13+
14+
if (remainingAmount < 0) {
15+
return;
16+
}
17+
18+
for (int i = startPoint; i < candidateList.length; i++) {
19+
combination.add(candidateList[i]);
20+
backtracking(remainingAmount - candidateList[i], combination, i, candidateList, resultList);
21+
combination.remove(combination.size() - 1);
22+
}
23+
}
24+
25+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
26+
ArrayList<Integer> initialCombination = new ArrayList<>();
27+
backtracking(target, initialCombination, 0, candidates, result);
28+
return result;
29+
}
30+
}

0 commit comments

Comments
 (0)