File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments