Last updated on January 20th, 2025 at 04:09 am
Here, we see the Combination Sum LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Array, Backtracking
Companies
Snapchat, Uber
Level of Question
Medium
Combination Sum LeetCode Solution
Table of Contents
1. Problem Statement
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.
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: []
2. Coding Pattern Used in Solution
The coding pattern used in all the provided implementations is “Backtracking”. Backtracking is a general algorithmic technique that involves exploring all potential solutions to a problem by building a solution incrementally and abandoning a solution as soon as it is determined that it cannot lead to a valid solution.
3. Code Implementation in Different Languages
3.1 Combination Sum C++
class Solution { public: vector<vector<int>> result; vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<int> curr; int sum = 0; int n = candidates.size(); comSum(curr, 0, sum, candidates, target, n); return result; } void comSum(vector<int> &curr, int curInd, int sum, vector<int> &candidates, int target, int n){ if(sum == target){ result.push_back(curr); return; } else if(sum > target){ return; } for(int i = curInd; i < n; i++){ curr.push_back(candidates[i]); sum += candidates[i]; comSum(curr, i, sum, candidates, target, n); sum -= candidates[i]; curr.pop_back(); } } };
3.2 Combination Sum Java
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(candidates); backtrack(list, new ArrayList<>(), candidates, target, 0); return list; } private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] candidates, int remain, int start){ if(remain < 0) return; else if(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < candidates.length; i++){ tempList.add(candidates[i]); backtrack(list, tempList, candidates, remain - candidates[i], i); // not i + 1 because we can reuse same elements tempList.remove(tempList.size() - 1); } } } }
3.3 Combination Sum JavaScript
var combinationSum = function(candidates, target) { var buffer = []; var result = []; search(0, target); return result; function search(startIdx, target) { if (target === 0) return result.push(buffer.slice()); if (target < 0) return; if (startIdx === candidates.length) return; buffer.push(candidates[startIdx]); search(startIdx, target - candidates[startIdx]); buffer.pop(); search(startIdx + 1, target); } };
3.4 Combination Sum Python
class Solution(object): def combinationSum(self, candidates, target): result = [] candidates = sorted(candidates) def dfs(remain, stack): if remain == 0: result.append(stack) return for item in candidates: if item > remain: break if stack and item < stack[-1]: continue else: dfs(remain - item, stack + [item]) dfs(target, []) return result
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(2t) | O(t) |
Java | O(2t) | O(t) |
JavaScript | O(2t) | O(t) |
Python | O(2t) | O(t) |
where T
is the target value.
- The time complexity is exponential because the algorithm explores all possible combinations of numbers that sum up to the target.
- The space complexity is linear because the recursion stack depth is proportional to the target value, and the current combination is stored in memory.
- The algorithm avoids duplicates by ensuring that combinations are built in a non-decreasing order.