Total Waviness of Numbers in Range II Problem
Description
LeetCode Problem 3753.
You are given two integers num1 and num2 representing an inclusive range [num1, num2].
The waviness of a number is defined as the total count of its peaks and valleys:
- A digit is a peak if it is strictly greater than both of its immediate neighbors.
- A digit is a valley if it is strictly less than both of its immediate neighbors.
- The first and last digits of a number cannot be peaks or valleys.
- Any number with fewer than 3 digits has a waviness of 0.
Return the total sum of waviness for all numbers in the range [num1, num2].
Example 1:
Input: num1 = 120, num2 = 130
Output: 3
Explanation:
In the range [120, 130]:
- 120: middle digit 2 is a peak, waviness = 1.
- 121: middle digit 2 is a peak, waviness = 1.
- 130: middle digit 3 is a peak, waviness = 1.
- All other numbers in the range have a waviness of 0.
Thus, total waviness is 1 + 1 + 1 = 3.
Example 2:
Input: num1 = 198, num2 = 202
Output: 3
Explanation:
In the range [198, 202]:
- 198: middle digit 9 is a peak, waviness = 1.
- 201: middle digit 0 is a valley, waviness = 1.
- 202: middle digit 0 is a valley, waviness = 1.
- All other numbers in the range have a waviness of 0.
Thus, total waviness is 1 + 1 + 1 = 3.
Example 3:
Input: num1 = 4848, num2 = 4848
Output: 2
Explanation:
Number 4848: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.
Constraints:
1 <= num1 <= num2 <= 10^15
Solution
Approach: Digit DP
The range is far too large to enumerate, so compute calc(x), the total waviness of all numbers in [1, x], and answer with calc(num2) - calc(num1 - 1).
calc(x) is a digit DP over the digits of x from the most significant one. The state is dfs(pos, prev2, prev1, started, limit): we are about to choose the digit at position pos, the previous two chosen digits are prev2 and prev1 (use 10 for "not chosen yet"), started says whether a non-zero leading digit has been placed, and limit says whether we are still bounded by the digits of x. It returns two values: how many numbers can be completed, and their total waviness.
When we pick the next digit d, the digit prev1 becomes a peak or valley if it is strictly greater than (or smaller than) both prev2 and d. That adds 1 to the waviness of every one of the c completions, so the contribution is c * 1 on top of the waviness inside the sub-problem. Results are memoized when limit is false.
Complexity: O(log x) time and space, with a constant factor of about 11 * 11 * 2 * 10 states per position.
from functools import lru_cache
class Solution:
def totalWaviness(self, num1: int, num2: int) -> int:
def calc(x: int) -> int:
if x < 0:
return 0
s = str(x)
@lru_cache(None)
def dfs(
pos: int, prev2: int, prev1: int, started: int, limit: bool
) -> tuple:
if pos == len(s):
return (started, 0)
up = int(s[pos]) if limit else 9
cnt = wav = 0
for d in range(up + 1):
nlimit = limit and d == up
add = 0
if started == 0:
if d == 0:
ns, np2, np1 = 0, 10, 10
else:
ns, np2, np1 = 1, 10, d
else:
ns, np2, np1 = 1, prev1, d
if prev2 != 10 and (
(prev1 > prev2 and prev1 > d)
or (prev1 < prev2 and prev1 < d)
):
add = 1
c, w = dfs(pos + 1, np2, np1, ns, nlimit)
cnt += c
wav += w + c * add
return cnt, wav
return dfs(0, 10, 10, 0, True)[1]
return calc(num2) - calc(num1 - 1)