3751. Total Waviness of Numbers in Range I Medium

@problem@discussion
#Math#Dynamic Programming#Enumeration



1/**
2 * [3751] Total Waviness of Numbers in Range I
3 *
4 * You are given two integers num1 and num2 representing an inclusive range [num1, num2].
5 * The waviness of a number is defined as the total count of its peaks and valleys:
6 * 
7 * 	A digit is a peak if it is strictly greater than both of its immediate neighbors.
8 * 	A digit is a valley if it is strictly less than both of its immediate neighbors.
9 * 	The first and last digits of a number cannot be peaks or valleys.
10 * 	Any number with fewer than 3 digits has a waviness of 0.
11 * Return the total sum of waviness for all numbers in the range [num1, num2].
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">num1 = 120, num2 = 130</span>
16 * Output: <span class="example-io">3</span>
17 * Explanation:
18 * In the range [120, 130]:
19 * 
20 * 	120: middle digit 2 is a peak, waviness = 1.
21 * 	121: middle digit 2 is a peak, waviness = 1.
22 * 	130: middle digit 3 is a peak, waviness = 1.
23 * 	All other numbers in the range have a waviness of 0.
24 * 
25 * Thus, total waviness is 1 + 1 + 1 = 3.
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input: <span class="example-io">num1 = 198, num2 = 202</span>
30 * Output: <span class="example-io">3</span>
31 * Explanation:
32 * In the range [198, 202]:
33 * 
34 * 	198: middle digit 9 is a peak, waviness = 1.
35 * 	201: middle digit 0 is a valley, waviness = 1.
36 * 	202: middle digit 0 is a valley, waviness = 1.
37 * 	All other numbers in the range have a waviness of 0.
38 * 
39 * Thus, total waviness is 1 + 1 + 1 = 3.
40 * </div>
41 * <strong class="example">Example 3:
42 * <div class="example-block">
43 * Input: <span class="example-io">num1 = 4848, num2 = 4848</span>
44 * Output: <span class="example-io">2</span>
45 * Explanation:
46 * Number 4848: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.
47 * </div>
48 *  
49 * Constraints:
50 * 
51 * 	1 <= num1 <= num2 <= 10^5
52 * 
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/total-waviness-of-numbers-in-range-i/
57// discuss: https://leetcode.com/problems/total-waviness-of-numbers-in-range-i/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61impl Solution {
62    pub fn total_waviness(num1: i32, num2: i32) -> i32 {
63        0
64    }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_3751() {
75    }
76}
77

Back
© 2026 bowen.ge All Rights Reserved.