-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathminji-go.java
31 lines (27 loc) · 1.09 KB
/
minji-go.java
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
/*
Problem: https://leetcode.com/problems/combination-sum/
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
Concept: Array, Backtracking
Time Complexity: O(Nᵀ), Runtime 2ms
Space Complexity: O(T), Memory 44.88MB
*/
class Solution {
public List<List<Integer>> answer = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
findCombination(candidates, target, new ArrayList<>(), 0);
return answer;
}
public void findCombination(int[] candidates, int target, List<Integer> combination, int idx) {
if(target == 0) {
answer.add(new ArrayList<>(combination));
return;
}
for(int i=idx; i<candidates.length; i++) {
if(candidates[i] > target) break;
combination.add(candidates[i]);
findCombination(candidates, target-candidates[i], combination, i);
combination.remove(combination.size()-1);
}
}
}