Last updated on October 10th, 2024 at 02:10 am
Here, We see Combination Sum LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.
List of all LeetCode Solution
Topics
Array, Backtracking
Companies
Snapchat, Uber
Level of Question
Medium
Combination Sum LeetCode Solution
Table of Contents
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: []
1. Combination Sum Leetcode Solution 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(); } } };
2. Combination Sum Leetcode Solution 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. Combination Sum Leetcode Solution 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); } };
4. Combination Sum Leetcode Solution 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