Link: https://leetcode.com/problems/total-waviness-of-numbers-in-range-i/description/
Problem
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 2 is a peak, waviness = 1.201: middle digit 2 is a peak, waviness = 1.202: 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 3:
Input: num1 = 4848, num2 = 4848
Output: 2
Explanation:
Number4848: 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^5$
Analysis
This problem costs you some time just to understand it and try things out. The most basic approach is to write a check function that counts the waves inside a single number, returns the result, and iterate over the range. That approach passes outright, so there is no need to overthink optimisation. A few things work in our favour:
- Convert the number to a string and walk element by element — comparing the characters ‘0’, ‘1’, ‘2’ … ‘9’ still compares correctly, because of their ASCII codes.
- The constraint tops out at 10^5, meaning the largest number takes about O(3) to count successfully — we skip the first and last digits — and iterating from num1 => num2 is at most 10000 numbers => roughly 30K operations =>
time-complexityis perfectly fine for brute force - O(n). - How small is that number, really?
A modern computer running JavaScript (V8 Engine) can handle around (100 million) simple operations per second. So handling under operations costs less than 2ms to 5ms on the LeetCode servers.
Final solution
| 🕒 Runtime | 💻 Memory |
|---|---|
| 17 ms | Beats 87.10% 🟩 | 62.68 MB | Beats 35.48% |
/**
* @param {number} num1
* @param {number} num2
* @return {number}
*/
function countWaves(num) {
if (num < 100) {
return 0;
}
let waves = 0;
const numString = String(num);
for (let i = 1; i < numString.length - 1; i++) {
if (numString[i] < numString[i-1] && numString[i] < numString[i+1]) {
waves++;
}
if (numString[i] > numString[i-1] && numString[i] > numString[i+1]) {
waves++;
}
}
return waves
}
var totalWaviness = function(num1, num2) {
let total = 0;
for (let i = num1; i <= num2; i++) {
total += countWaves(i)
}
return total
};

