Combination Sum 1
Table of contents
Problem
Given an array of distinct integers candidates
and a target integer target
, return a list of all unique combinations of candidates
where the chosen numbers sum to target
. You may return the combinations in any order.
The same number may be chosen from candidates
an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
The test cases are generated such that the number of unique combinations that sum up to target
is less than 150
combinations for the given input.
Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
Example 2:
Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input: candidates = [2], target = 1
Output: []
Constraints:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
All elements of
candidates
are distinct.1 <= target <= 40
Solution
Recursive Approach
This is similar to the subsequence patterns (link). We follow a similar template here. Initially, the subsequence template includes the index
and exclude the index
, then directly moves on to the next index index+1
. Here, everything is the same except after including the index, we also stay on the same index to find all the possible subsets before moving on to the next index.
Time Complexity: O(2^t n) k
Single Selection Case: If each index is included only once, the total number of possible subarrays is (2^n). These subarrays are stored in the main answer array by deep copying, which is equivalent to creating a new subarray of average length (k). Therefore, the time complexity is (O(2^n) * k).
Multiple Selections: In this case, since the index can be picked or not picked an average of (t) times, the total number of nodes will be (2^t). Given there are (n) elements, the time complexity becomes (2^t * n). The factor (k) accounts for the time taken to copy the array.
class Solution {
public void findAll(int[] candidates, int target, int index, int currentSum, List<Integer> currentSet, List<List<Integer>> ans){
if(index==candidates.length){
if(currentSum == target){
ans.add(new ArrayList<>(currentSet));
}
return;
}
if(currentSum>target) return;
currentSum+=candidates[index];
currentSet.add(candidates[index]);
findAll(candidates, target, index, currentSum, currentSet, ans);
currentSum-=candidates[index];
currentSet.remove(currentSet.size()-1);
findAll(candidates, target, index+1, currentSum, currentSet, ans);
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
findAll(candidates, target, 0, 0, new ArrayList<>(), ans);
return ans;
}
}
Subscribe to my newsletter
Read articles from Chetan Datta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Chetan Datta
Chetan Datta
I'm someone deeply engrossed in the world of software developement, and I find joy in sharing my thoughts and insights on various topics. You can explore my exclusive content here, where I meticulously document all things tech-related that spark my curiosity. Stay connected for my latest discoveries and observations.