-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathCombinationSum2.java
More file actions
38 lines (32 loc) · 1.26 KB
/
CombinationSum2.java
File metadata and controls
38 lines (32 loc) · 1.26 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
import java.util.ArrayList;
import java.util.List;
/*
* Combination Sum (Variation) - LeetCode 39
* Approach: Backtracking. Try all combinations recursively, allowing repeated use of elements.
* Time Complexity: O(2^n * k), where n is the number of candidates and k is the average length of a combination.
* Space Complexity: O(k * x), where x is the number of valid combinations.
* LeetCode Link: https://leetcode.com/problems/combination-sum/
*/
public class CombinationSum2 {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, 0, target, new ArrayList<Integer>(), result);
return result;
}
private void helper(int[] candidates, int pivot, int target, List<Integer> path, List<List<Integer>> result) {
if (target < 0) return;
if (target == 0) {
result.add(new ArrayList<>(path));
return;
}
//base
for (int i = pivot; i < candidates.length; i++) {
//action
path.add(candidates[i]);
//recurse
helper(candidates, i, target - candidates[i], path, result);
//backtrack
path.remove(path.size() - 1);
}
}
}