leetcode/39_combination_sum
39_combination_sum
dz /
leetcode /
39_combination_sum
Summary
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.
Node Tree
Nodes
dynamic_programming
|
content
|
This is probably a dynamic programming slash recursive backtracking solution. You can always break down a problem into "given an initial value X, find all the numbers that can add to less than or equal to target". Repeat that recursively, and keep track of numbers so far. Recursion ends when there is no number that can be less than or equal to target. Add to list if it is exactly the target. If sum is less than target, recurse.
|
children
|
handling_distinction, use_of_remainder (My logic was slightly different, opting to keep the current,sum rather than remainder.), approach_1_backtracking (I also mentioned "backtracking" here, which is probably,closer to what it actually is)
|
parents
|
intuition
|
handling_distinction
|
content
|
In past problems, there have been solutions that have really elegantly handled distinctness. I forget them now. Perhaps it's how you set up the iteration for finding the numbers. When you check for matching sums, you're going through each number once. I'm willing to believe that ordering prevents duplicates from happening.
|
children
|
select_candidates_in_order (My hunch about doing things in order was correct)
|
parents
|
dynamic_programming, intuition
|
backtracking
|
content
|
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems. The idea is that it incrementally builds candidates to the solutions, and abandons a candidate ("backtrack") as soon as it determines that this candidate is not a final solution.
|
children
|
backtracking_wiki
|
parents
|
approach_1_backtracking
|
use_of_remainder
|
content
|
Use of remainder variable to keep track of how much to the target is remaining. Base case remain==0 means a combination has been found, remain < 0 means value has been exceeded and exploration ceases.
|
parents
|
dynamic_programming
|
time_complexity
|
content
|
O(N^(T/M + 1)) where N is number of candidates, T is target value, and M is the minimal value among the candidates.
|
children
|
space_complexity (related)
|
parents
|
approach_1_backtracking
|
remarks
|
not sure how this was derived, but this was an interestingly precise O number.
|
intuition_basically_correct
|
content
|
I think my intuition for this one was basically correct for this, though I thought the solution was an example of dynamic programming, not backtracking, even though I wrote down both. there's clearly a bunch of algorithms that make use of the backtracking pattern, so it'll be worth looking into in greater detail.
|
parents
|
approach_1_backtracking, intuition
|