-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathCombinationSum.java
More file actions
71 lines (55 loc) · 2.31 KB
/
CombinationSum.java
File metadata and controls
71 lines (55 loc) · 2.31 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Time Complexity : O(2^(m+n)) where m is the length of candidates ad n is the target value.
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Approach : I followed 0/1 recursion with backtracking and while the base condition is met, before storing the result we store a deep
// copy of it so that the rest of recursion happens without any issues. In no choose condition, index needs to be increased while in
// choose condition we dont have to increase index as it can have duplicates as well to reach the target sum. Once we find the result or we
// reach the end of the recursive function, we remove the last element in the path so that it can continue recursion.
class Solution {
List<List<Integer>> result;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
this.result = new ArrayList<>();
helper(candidates, target, 0, new ArrayList<>());
return result;
}
private void helper(int[] candidates, int target, int index, List<Integer> path) {
if (target == 0) {
result.add(new ArrayList<>(path)); //copy of path
return;
}
if (target < 0 || index == candidates.length) {
return;
}
//no choose
helper(candidates, target, index + 1, path);
//choose
path.add(candidates[index]); //add to path first
helper(candidates, target - candidates[index], index, path);
//backtracking
path.remove(path.size() - 1);
}
}
//for loop based recursion
class Solution {
List<List<Integer>> result;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
this.result = new ArrayList<>();
helper(candidates, target, 0, new ArrayList<>());
return result;
}
private void helper(int[] candidates, int target, int index, List<Integer> path) {
if (target == 0) {
result.add(new ArrayList<>(path)); //copy of path
return;
}
if (target < 0 || index == candidates.length) {
return;
}
for (int i = index; i < candidates.length; i++) {
path.add(candidates[i]);
helper(candidates, target - candidates[i], i, path);
//backtracking
path.remove(path.size() - 1);
}
}
}