LeetCode Entry
3751. Total Waviness of Numbers in Range I
Count hills and valleys base10 in a..b
3751. Total Waviness of Numbers in Range I medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1380
Problem TLDR
Count hills and valleys base10 in a..b
Intuition
Brute-force
Approach
- Rust: itertools allows for tuple_windows
Complexity
-
Time complexity: \(O(nlogn)\)
-
Space complexity: \(O(logn)\)
Code
fun totalWaviness(a: Int, b: Int) =
(a..b).sumOf {"$it".windowed(3).count {(it[0]-it[1])*(it[2]-it[1])>0}}
pub fn total_waviness(a: i32, b: i32) -> i32 {
(a..=b).map(|x|x.to_string().bytes().tuple_windows()
.filter(|(a,b,c)|a>b&&c>b||a<b&&c<b).count()as i32).sum::<i32>()
}
Comments