Problem · Dynamic Programming
Count Ordered Size Sequences
Learn this problemProblem statement
Given a positive target n and an array of distinct positive integers sizes, count the ordered sequences of values from sizes whose sum is exactly n.
Each size may be used any number of times. Two sequences are different when they differ at any position, so order matters.
Return the number of valid sequences.
Function
countOrderedSums(n: int, sizes: int[]) → longExamples
Example 1
n = 3sizes = [1,2]return = 3The valid ordered sequences are [1,1,1], [1,2], and [2,1].
Example 2
n = 4sizes = [1,3]return = 3The sequences are [1,1,1,1], [1,3], and [3,1].
Constraints
1 <= n <= 601 <= sizes.length <= 601 <= sizes[i] <= n- All values in
sizesare distinct. - The answer fits in a signed 64-bit integer.