Skip to content

Commit 8561368

Browse files
committed
combination sum
1 parent d174b65 commit 8561368

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

combination-sum/se6816.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
중복 조합을 통해 조건에 맞는 조합을 찾는 방식
3+
candidates의 길이 -> N
4+
시간 복잡도 : O(2^N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
List<List<Integer>> list;
9+
List<Integer> dataList;
10+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
11+
Arrays.sort(candidates);
12+
list = new ArrayList<>();
13+
dataList = new ArrayList<>();
14+
execute(candidates, 0, 0, target);
15+
return list;
16+
}
17+
18+
public void execute(int[] candidates, int idx, int sum, int target) {
19+
if(idx >= candidates.length) {
20+
return;
21+
}
22+
23+
if(sum == target) {
24+
list.add(new ArrayList<>(dataList));
25+
return;
26+
}
27+
28+
if(sum+candidates[idx] > target) {
29+
return;
30+
}
31+
32+
33+
34+
35+
dataList.add(candidates[idx]);
36+
execute(candidates, idx, sum + candidates[idx], target);
37+
dataList.remove(dataList.size()-1);
38+
execute(candidates, idx+1, sum, target);
39+
}
40+
}

0 commit comments

Comments
 (0)