You get a list of positive integers candidates, which may contain repeats, and a positive integer target. Find every combination of the numbers whose sum is exactly target.
Each position in candidates can be used at most once in a combination, so a value can appear in a combination only as many times as it appears in candidates. Two combinations that hold the same values with the same counts are the same combination, even when they use different positions: list each one only once.
You may return the combinations in any order, and the numbers inside each combination in any order too.
[2,2,3] is allowed because 2 appears twice in the list. [1,2,4] can be built with either 2, but it is listed once.
Any two of the three 3s give 6, and that counts as one combination. [3,3] plus the 1 would be 7.
[]
Every number is already bigger than target.
Constraints
1 <= candidates.length <= 1001 <= candidates[i] <= 501 <= target <= 30Building every combination in sorted order stops [1,2] and [2,1] from both appearing. The harder part is repeats in the input: two copies of 2 must not each start their own copy of the same combinations.
A combination is a bag of numbers: what matters is how many of each value it holds, not the order and not which copy of a value it came from. So there are two ways to list the same bag twice, and the search has to block both.
Time O(n · K)Space O(n) besides the answer
The full walkthrough and code are part of Premium