An integer is called good if its digits form a strictly monotone sequence, meaning the digits are strictly increasing or strictly decreasing. All single-digit integers are considered good.
An integer is called fancy if it is good, or if the sum of its digits is good.
Return an integer representing the number of fancy integers in the range [l, r] (inclusive).
A sequence is said to be strictly increasing if each element is strictly greater than its previous one (if exists).
A sequence is said to be strictly decreasing if each element is strictly less than its previous one (if exists).
Β
Example 1:
Input:l = 8, r = 10
Output:3
Explanation:
8 and 9 are single-digit integers, so they are good and therefore fancy.
10 has digits [1, 0], which form a strictly decreasing sequence, so 10 is good and thus fancy.
Therefore, the answer is 3.
Example 2:
Input:l = 12340, r = 12341
Output:1
Explanation:
12340
12340 is not good because [1, 2, 3, 4, 0] is not strictly monotone.
The digit sum is 1 + 2 + 3 + 4 + 0 = 10.
10 is good as it has digits [1, 0], which is strictly decreasing. Therefore, 12340 is fancy.
12341
12341 is not good because [1, 2, 3, 4, 1] is not strictly monotone.
The digit sum is 1 + 2 + 3 + 4 + 1 = 11.
11 is not good as it has digits [1, 1], which is not strictly monotone. Therefore, 12341 is not fancy.
Therefore, the answer is 1.
Example 3:
Input:l = 123456788, r = 123456788
Output:0
Explanation:
123456788 is not good because its digits are not strictly monotone.
The digit sum is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 8 = 44.
44 is not good as it has digits [4, 4], which is not strictly monotone. Therefore, 123456788 is not fancy.
Therefore, the answer is 0.
Β
Constraints:
1 <= l <= r <= 1015
Solutions
Solution 1: Digit DP
We first define a function \(\text{check}(s)\) to determine whether an integer \(s\) is a good number. For \(s < 100\), we only need to check whether \(s\) is a multiple of 11; if so, \(s\) is not a good number. For \(s \geq 100\), we need to check whether the digits of \(s\) form a strictly monotonic sequence, i.e., strictly increasing or strictly decreasing. Since the range of digit sums is small, when the digit sum exceeds \(100\), we only need to check the relationship between the tens digit and the units digit of the digit sum.
Next, we use digit DP to count the number of fancy numbers in the interval \([l, r]\). We define a recursive function \(\text{dfs}(pos, s, prev, st, lim)\), where:
Integer \(pos\) represents the current digit position being processed, from high to low.
Integer \(s\) represents the current digit sum.
Integer \(prev\) represents the value of the previous digit.
Integer \(st\) represents the state of the current digit sequence, where state \(0\) means there is at most one digit so far, state \(1\) means strictly increasing, state \(2\) means strictly decreasing, and state \(3\) means not strictly monotonic.
Boolean \(lim\) indicates whether the current digit is constrained by the upper bound.
In the recursive function, if \(pos\) exceeds the length of the number, we have finished processing a number. If the state \(st\) is not equal to \(3\), the number is a good number and we return \(1\); otherwise, we call \(\text{check}(s)\) to determine whether the digit sum is a good number, returning \(1\) if it is, and \(0\) otherwise.
During the recursion, we enumerate the range of the current digit: if \(lim\) is true, the range is \([0, \text{num}[pos]]\); otherwise, it is \([0, 9]\). For each value, we update the state \(st\) and recursively call \(\text{dfs}\) to process the next digit.
Finally, we compute the count of fancy numbers in \([0, r]\) and \([0, l-1]\) separately, and the answer is their difference.
The time complexity is \(O(D^3 \times \log^2 r)\) and the space complexity is \(O(D^2 \times \log^2 r)\), where \(D = 10\).