Count Nondecreasing Digit-Sum Arrays
Problem statement
Given an integer array requiredSum of length n, count the arrays result of length n that satisfy all of the following rules:
resultis nondecreasing, soresult[i] <= result[i + 1]for every validi.- For every index
i, the sum of the decimal digits ofresult[i]equalsrequiredSum[i]. - Every value in
resultis between1and5000, inclusive.
Return the number of distinct valid arrays modulo 10^9 + 7.
Function
countDigitSumArrays(requiredSum: int[]) → intExamples
Example 1
requiredSum = [1]return = 4The valid one-element arrays are [1], [10], [100], and [1000].
Example 2
requiredSum = [1,1]return = 10The only eligible values are 1, 10, 100, and 1000. Choosing any two of them with repetition and writing them in nondecreasing order gives 10 arrays.
Example 3
requiredSum = [31,1]return = 0The only value at most 5000 with digit sum 31 is 4999. No value greater than or equal to 4999 has digit sum 1, so no valid nondecreasing array exists.
Constraints
1 <= requiredSum.length <= 5000.1 <= requiredSum[i] <= 31.