Combination Sum
DESCRIPTION (credit Leetcode.com)
Given an array of distinct integers candidates and a target integer target, generate all unique combinations of candidates which sum to target. The combinations may be returned in any order, and the same number may be chosen from candidates an unlimited number of times.
EXAMPLES
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.
Solution
def combinationSum(candidates, target):def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if candidates[i] > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()returncandidates.sort()result = []backtrack(0, [], target)return result
combination sum
0 / 56
1x
Explanation
This solution uses backtracking to find all the combinations of numbers that sum up to the target. The backtracking function is a recursive function that uses depth-first search to explore all possible combinations of candidates that sum up to the target. Each call to backtracking takes in parameters start, combo, and current_target. The start parameter is the index of the current candidate, combo is the current combination of numbers, and the current_target parameter is the target we are trying to hit (equal to target - the sum of combo).
The first step is to sort candidates, which makes it easy to tell when the current search combination exceeds the target, and can be pruned.
def combinationSum(candidates, target):def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if candidates[i] > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()returncandidates.sort()result = []backtrack(0, [], target)return result
combination sum
0 / 1
1x
Next, we kick off the search. Each call to the backtracking function first checks if the current_target is equal to 0. If so, that means the current value for combo is a valid solution, so we add it to the result. If not, the function iterates through the candidates starting from the start index. For each candidate, we add it to the current combination and recursively call the backtracking function with the updated current_target. Since we can use the same candidate multiple times, we pass the start index as the current index to the next call.
def combinationSum(candidates, target):def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if candidates[i] > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()returncandidates.sort()result = []backtrack(0, [], target)return result
sort candidates
0 / 10
1x
If at any point the current_target becomes less than 0, we know that the current combo is not a valid solution, so we backtrack to the previous call.
For the backtracking step, we remove the last number from the current combination and try the next candidate. This process continues until we have tried all the candidates.
def combinationSum(candidates, target):def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if candidates[i] > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()returncandidates.sort()result = []backtrack(0, [], target)return result
def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if curr > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()return
def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if curr > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()return
def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if curr > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()return
def backtrack(start, combo, current_target):if current_target == 0:result.append(list(combo))returnfor i in range(start, len(candidates)):curr = candidates[i]if curr > current_target:returncombo.append(curr)backtrack(i, combo, current_target - curr)combo.pop()return
i = 0
0 / 2
1x
© 2024 Optick Labs Inc. All rights reserved.
Loading comments...