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:
[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:
[1, 2, 3, 4, 0] is not strictly monotone.1 + 2 + 3 + 4 + 0 = 10.[1, 0], which is strictly decreasing. Therefore, 12340 is fancy.[1, 2, 3, 4, 1] is not strictly monotone.1 + 2 + 3 + 4 + 1 = 11.[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:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 8 = 44.[4, 4], which is not strictly monotone. Therefore, 123456788 is not fancy.Therefore, the answer is 0.
Constraints:
1 <= l <= r <= 1015Solutions for this problem are being prepared.
Try solving it yourselfPractice Count Fancy Numbers in a Range with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor