-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSumII.java
More file actions
162 lines (132 loc) Β· 7.21 KB
/
CombinationSumII.java
File metadata and controls
162 lines (132 loc) Β· 7.21 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package Algorithms.BackTracking;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 11 May 2025
* @link https://leetcode.com/problems/combination-sum-ii/
*
* Same like {@link Algorithms.BackTracking.CombinationSum -- Combination Sum I} but sort arr & and skip duplicates
*/
public class CombinationSumII {
public static void main(String[] args) {
int[] candidates = {10,1,2,7,6,1,5};
int target = 8;
System.out.println("combinationSum2_1 => " + combinationSum2_1(candidates, target));
System.out.println("combinationSum2_2 => " + combinationSum2_2(candidates, target));
}
/**
* APPROACH 1
* @TimeComplexity O(nlogn) + O(2^T), where T is the target value.
* @SpaceComplexity O(2^T)
*
* Here both left and right children index++ -- cause no dups
* and add sum++ only in left child
*
* NOTE:
* 1. If you want duplicate index numbers then don't index++ in left child π₯
*
* when candidates=[10,1,2,7,6,1,5] and target=8
* after sort candidates=[1,1,2,5,6,7,10]
* The reason we sorted cause we get [1,7], [7,1], but in the given sum the order doesn't matter
* And we skip dups because [1,7] and [1,7] -- here first 1 is 0th index and second 1 is 1st index. but we don't want multiple [1,7]s
* β
* β nextI=0 β
* i=0, nextI=1 [] i=0, nextI=2 ---> to skip duplicates
* __________________________|____________________
* β | | β
* i=1,nI=2 [1] i=1,nI=2 i=2, nI=3 [] i=2, nextI=3
* _______________|_____________ _______________|_____________
* β | | β β | |
* 2,3 [1,1] 2,3 2,3 [1] 2,3 3,4 [2] 3,4 3,4 [] 3,4
* _________|_________ _________|_________ _______|_______ _______|_______
* | | | | | | | |
* [1,1,2] [1,1] [1,2] [1] [2,5] [2] [5] []
*
*
*
* []
* __________________________|____________________
* | |
* [1] []
* _______________|_____________ _______________|_____________
* | | | |
* [1,1] [1] [2] []
* _________|_________ _________|_________ _______|_______ _______|_______
* | | | | | | | |
* [1,1,2] [1,1] [1,2] [1] [2,5] [2] [5] []
*
*/
public static List<List<Integer>> combinationSum2_1(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> lst = new ArrayList<>();
int nextI = 0;
backtrack(candidates, lst, new ArrayList<>(), target, nextI);
return lst;
}
private static void backtrack(int[] candidates, List<List<Integer>> lst, List<Integer> subLst, int runningDiff, int i) { // currI
if(runningDiff == 0) {
lst.add(new ArrayList<>(subLst));
return;
}
else if(runningDiff < 0 || i>=candidates.length) return;
subLst.add(candidates[i]); // currI
int nextI = i+1;
backtrack(candidates, lst, subLst, runningDiff-candidates[i], nextI); // nextI
subLst.remove(subLst.size()-1);
while(i+1<candidates.length && candidates[i]==candidates[i+1]) i++; // last duplicates
nextI = i+1;
backtrack(candidates, lst, subLst, runningDiff, nextI); // nextI
}
public static List<List<Integer>> combinationSum2_2(int[] candidates, int target) {
List<List<Integer>> lst = new ArrayList<>();
Arrays.sort(candidates);
backtrack(candidates, target, new ArrayList<>(), lst, 0); // start == index i.e toIndex
return lst;
}
public static void backtrack(int[] candidates, int target, List<Integer> subLst, List<List<Integer>> lst, int i) {
if (target == 0) {
lst.add(new ArrayList<>(subLst));
return;
}
if (i >= candidates.length || target < 0) return;
for (int j = i; j < candidates.length; j++) {
if (j > i && candidates[j] == candidates[j - 1]) continue; // skip duplicates & note that j > i condition skip 1st iteration
subLst.add(candidates[j]);
backtrack(candidates, target - candidates[j], subLst, lst, j + 1); // toIndex or nextI
subLst.remove(subLst.size() - 1);
}
}
Set<String> set = new HashSet<>();
public List<List<Integer>> combinationSum2WorkingButTLE(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> lst = new ArrayList<>();
backtrack(candidates, lst, new ArrayList<>(), target, 0, new HashMap<>());
return lst;
}
private void backtrack(int[] cands, List<List<Integer>> lst, List<Integer> subLst, int runningDiff, int fromIndex, Map<Integer, Integer> counter) {
if(runningDiff < 0) return;
else if(runningDiff == 0) {
StringBuilder sb = new StringBuilder();
for(int k: counter.keySet()) {
int v=counter.get(k);
sb.append(k).append(":").append(v).append(",");
}
if(set.add(sb.toString())) lst.add(new ArrayList<>(subLst));
return;
}
for(int i=fromIndex; i<cands.length; i++) {
subLst.add(cands[i]);
counter.merge(cands[i], 1, Integer::sum);
backtrack(cands, lst, subLst, runningDiff-cands[i], i+1, counter);
subLst.remove(subLst.size()-1);
counter.merge(cands[i], -1, Integer::sum);
if(counter.get(cands[i])==0) counter.remove(cands[i]);
}
}
}