|
| 1 | +/** |
| 2 | + * Time Complexity: O(2^n) - Exponential in worst case |
| 3 | + * Space Complexity: O(target) - Recursion stack depth |
| 4 | + */ |
| 5 | +class Solution { |
| 6 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 7 | + List<List<Integer>> result = new ArrayList<>(); |
| 8 | + Arrays.sort(candidates); // Sort to group duplicates |
| 9 | + backtrack(candidates, target, 0, new ArrayList<>(), result); |
| 10 | + return result; |
| 11 | + } |
| 12 | + |
| 13 | + private void backtrack(int[] candidates, int target, int start, List<Integer> current, List<List<Integer>> result) { |
| 14 | + if (target == 0) { |
| 15 | + result.add(new ArrayList<>(current)); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + if (target < 0) { |
| 20 | + return; // Prune invalid path |
| 21 | + } |
| 22 | + |
| 23 | + for (int i = start; i < candidates.length; i++) { |
| 24 | + // Skip duplicates at same level |
| 25 | + if (i > start && candidates[i] == candidates[i - 1]) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + if (candidates[i] > target) { |
| 30 | + break; // Prune since array is sorted |
| 31 | + } |
| 32 | + |
| 33 | + current.add(candidates[i]); |
| 34 | + backtrack(candidates, target - candidates[i], i + 1, current, result); |
| 35 | + current.remove(current.size() - 1); // Backtrack |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Alternative approach using Set |
| 41 | +class SolutionSet { |
| 42 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 43 | + Set<List<Integer>> result = new HashSet<>(); |
| 44 | + Arrays.sort(candidates); |
| 45 | + backtrack(candidates, target, 0, new ArrayList<>(), result); |
| 46 | + return new ArrayList<>(result); |
| 47 | + } |
| 48 | + |
| 49 | + private void backtrack(int[] candidates, int target, int start, List<Integer> current, Set<List<Integer>> result) { |
| 50 | + if (target == 0) { |
| 51 | + result.add(new ArrayList<>(current)); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (target < 0) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + for (int i = start; i < candidates.length; i++) { |
| 60 | + if (candidates[i] > target) { |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + current.add(candidates[i]); |
| 65 | + backtrack(candidates, target - candidates[i], i + 1, current, result); |
| 66 | + current.remove(current.size() - 1); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Alternative approach using frequency map |
| 72 | +class SolutionFrequency { |
| 73 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 74 | + Map<Integer, Integer> freq = new HashMap<>(); |
| 75 | + for (int num : candidates) { |
| 76 | + freq.put(num, freq.getOrDefault(num, 0) + 1); |
| 77 | + } |
| 78 | + |
| 79 | + List<List<Integer>> result = new ArrayList<>(); |
| 80 | + backtrack(freq, target, new ArrayList<>(), result); |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + private void backtrack(Map<Integer, Integer> freq, int target, List<Integer> current, List<List<Integer>> result) { |
| 85 | + if (target == 0) { |
| 86 | + result.add(new ArrayList<>(current)); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + if (target < 0) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + for (int num : freq.keySet()) { |
| 95 | + if (freq.get(num) > 0 && num <= target) { |
| 96 | + current.add(num); |
| 97 | + freq.put(num, freq.get(num) - 1); |
| 98 | + backtrack(freq, target - num, current, result); |
| 99 | + freq.put(num, freq.get(num) + 1); |
| 100 | + current.remove(current.size() - 1); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Alternative approach using iterative method |
| 107 | +class SolutionIterative { |
| 108 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 109 | + List<List<Integer>> result = new ArrayList<>(); |
| 110 | + Stack<List<Integer>> stack = new Stack<>(); |
| 111 | + Stack<Integer> targetStack = new Stack<>(); |
| 112 | + Stack<Integer> indexStack = new Stack<>(); |
| 113 | + |
| 114 | + stack.push(new ArrayList<>()); |
| 115 | + targetStack.push(target); |
| 116 | + indexStack.push(0); |
| 117 | + |
| 118 | + while (!stack.isEmpty()) { |
| 119 | + List<Integer> current = stack.pop(); |
| 120 | + int remainingTarget = targetStack.pop(); |
| 121 | + int startIndex = indexStack.pop(); |
| 122 | + |
| 123 | + if (remainingTarget == 0) { |
| 124 | + result.add(current); |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + if (remainingTarget < 0) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + for (int i = startIndex; i < candidates.length; i++) { |
| 133 | + if (i > startIndex && candidates[i] == candidates[i - 1]) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if (candidates[i] > remainingTarget) { |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + List<Integer> newCombination = new ArrayList<>(current); |
| 142 | + newCombination.add(candidates[i]); |
| 143 | + stack.push(newCombination); |
| 144 | + targetStack.push(remainingTarget - candidates[i]); |
| 145 | + indexStack.push(i + 1); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return result; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// More concise version |
| 154 | +class SolutionConcise { |
| 155 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 156 | + List<List<Integer>> result = new ArrayList<>(); |
| 157 | + Arrays.sort(candidates); |
| 158 | + backtrack(candidates, target, 0, new ArrayList<>(), result); |
| 159 | + return result; |
| 160 | + } |
| 161 | + |
| 162 | + private void backtrack(int[] candidates, int target, int start, List<Integer> current, List<List<Integer>> result) { |
| 163 | + if (target == 0) { |
| 164 | + result.add(new ArrayList<>(current)); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + if (target < 0) return; |
| 169 | + |
| 170 | + for (int i = start; i < candidates.length; i++) { |
| 171 | + if (i > start && candidates[i] == candidates[i - 1]) continue; |
| 172 | + if (candidates[i] > target) break; |
| 173 | + |
| 174 | + current.add(candidates[i]); |
| 175 | + backtrack(candidates, target - candidates[i], i + 1, current, result); |
| 176 | + current.remove(current.size() - 1); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// Using memoization |
| 182 | +class SolutionMemo { |
| 183 | + private Map<String, List<List<Integer>>> memo = new HashMap<>(); |
| 184 | + |
| 185 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 186 | + Arrays.sort(candidates); |
| 187 | + return backtrack(candidates, target, 0); |
| 188 | + } |
| 189 | + |
| 190 | + private List<List<Integer>> backtrack(int[] candidates, int target, int start) { |
| 191 | + String key = target + "," + start; |
| 192 | + if (memo.containsKey(key)) { |
| 193 | + return memo.get(key); |
| 194 | + } |
| 195 | + |
| 196 | + List<List<Integer>> result = new ArrayList<>(); |
| 197 | + |
| 198 | + if (target == 0) { |
| 199 | + result.add(new ArrayList<>()); |
| 200 | + return result; |
| 201 | + } |
| 202 | + |
| 203 | + if (target < 0) { |
| 204 | + return result; |
| 205 | + } |
| 206 | + |
| 207 | + for (int i = start; i < candidates.length; i++) { |
| 208 | + if (i > start && candidates[i] == candidates[i - 1]) { |
| 209 | + continue; |
| 210 | + } |
| 211 | + |
| 212 | + if (candidates[i] > target) { |
| 213 | + break; |
| 214 | + } |
| 215 | + |
| 216 | + List<List<Integer>> subResults = backtrack(candidates, target - candidates[i], i + 1); |
| 217 | + for (List<Integer> subResult : subResults) { |
| 218 | + List<Integer> newResult = new ArrayList<>(subResult); |
| 219 | + newResult.add(candidates[i]); |
| 220 | + result.add(newResult); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + memo.put(key, result); |
| 225 | + return result; |
| 226 | + } |
| 227 | +} |
0 commit comments