Loading...
Given an array values of distinct positive integers and a positive integer target, return every combination of values whose sum is exactly target. Each value may be used any number of times in a combination; two combinations are different when they use at least one value a different number of times.
To make the answer unique, return the combinations in the following canonical order:
If no combination sums to target, return an empty array.
Input: values = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation: 2 + 2 + 3 = 7 (the value 2 is used twice) and 7 = 7. No other combination reaches 7.
Input: values = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]
Explanation: Three combinations sum to 8, listed in lexicographic order.
Input: values = [2], target = 1
Output: []
Explanation: No multiple of 2 equals 1.
values.length ≤30values[i] ≤40values are distinct.target ≤40Click "Run" to test with sample cases or "Submit" to run all tests.