Problem · Array
Count Ordered Combination Sums
Learn this problemProblem statement
Given an array of distinct positive integers candidates and a positive integer target, return the number of nonempty ordered sequences whose values sum to target.
- You may reuse a candidate any number of times.
- Two sequences with the same values in different orders count separately.
Function
countOrderedCombinationSums(candidates: int[], target: int) → intExamples
Example 1
candidates = [1,2,3]target = 4return = 7The sequences are [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3], and [3,1].
Example 2
candidates = [2,4]target = 6return = 3The valid ordered sequences are [2,2,2], [2,4], and [4,2].
Example 3
candidates = [5]target = 3return = 0No sequence of reusable 5s can sum to 3.
Constraints
1 <= candidates.length <= 200.1 <= candidates[i] <= 1000.- All values in
candidatesare distinct. 1 <= target <= 1000.- The correct answer fits a signed 32-bit integer.