Count Fancy Numbers in a Range Problem
Description
LeetCode Problem 3869.
You are given two integers l and r.
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 <= 10^15
Solution
Approach: Digit DP
Count fancy numbers in [0, x] with a digit DP and return calc(r) - calc(l - 1).
A helper check(s) tells whether a digit sum s is good. The digit sum is at most 9 * 15 = 135: below 100 it is good unless both digits are equal (a multiple of 11), and from 100 up it is good only when the three digits are strictly increasing, i.e. 1 < tens < ones.
The DP state is dfs(pos, sum, prev, st, lim): the digit position, the digit sum so far, the previous digit, the shape of the digits so far (0 at most one digit, 1 increasing, 2 decreasing, 3 neither) and whether we are still tied to the bound. At the end, a number whose shape is not 3 is good, so it counts; otherwise it counts only if check(sum) holds. States without the bound are memoized.
Complexity: About O(D^3 * log^2 r) time and O(D^2 * log^2 r) space, where D = 10 is the number of digit values.
from functools import lru_cache
class Solution:
def countFancy(self, l: int, r: int) -> int:
def check(s: int) -> bool:
if s < 100:
return s % 11 != 0
return 1 < s // 10 % 10 < s % 10
@lru_cache(None)
def dfs(pos: int, s: int, prev: int, st: int, lim: bool) -> int:
if pos >= len(num):
if st != 3:
return 1
return int(check(s))
up = int(num[pos]) if lim else 9
res = 0
for i in range(up + 1):
nxt_st = st
if st == 0:
if prev == 0:
nxt_st = 0
elif i > prev:
nxt_st = 1
elif i < prev:
nxt_st = 2
else:
nxt_st = 3
elif st == 1:
if i > prev:
nxt_st = 1
else:
nxt_st = 3
elif st == 2:
if i < prev:
nxt_st = 2
else:
nxt_st = 3
else:
nxt_st = 3
res += dfs(pos + 1, s + i, i, nxt_st, lim and i == up)
return res
num = str(l - 1)
a = dfs(0, 0, 0, 0, True)
dfs.cache_clear()
num = str(r)
b = dfs(0, 0, 0, 0, True)
return b - a