Introduction
LeetCode 3751 introduces an interesting digit-based pattern problem where we calculate the total waviness of numbers inside a given range.
This problem combines:
- Digit traversal
- Pattern recognition
- Peaks and valleys logic
- String manipulation
- Brute force iteration
The problem is straightforward once we clearly understand how peaks and valleys work.
Problem Statement
You are given two integers:
representing the inclusive range:
For every number:
- A digit is called a peak if it is strictly greater than both neighbors.
- A digit is called a valley if it is strictly smaller than both neighbors.
The first and last digits can never be peaks or valleys.
Return the total waviness across all numbers in the range.
Example
Input
Output
Explanation
Numbers contributing to waviness:
Total:
Understanding Peaks and Valleys
Peak Condition
A digit is a peak if:
Example:
Here:
So 8 is a peak.
Valley Condition
A digit is a valley if:
Example:
Here:
So 0 is a valley.
Key Observation
We only need to check:
because:
- First digit has no left neighbor
- Last digit has no right neighbor
Intuition
The simplest way:
- Iterate through every number in the range
- Convert number into characters
- Check each middle digit
- Count peaks and valleys
- Add to final answer
Since constraints are small:
a brute force approach works efficiently.
Java Solution
Step-by-Step Explanation
Step 1: Iterate Through Range
Process every number.
Step 2: Convert Number to Character Array
This makes digit comparison easier.
Step 3: Check Middle Digits
Skip first and last digits.
Step 4: Check Peak or Valley
If true:
Dry Run
Input
Number 198
Digits:
Check 9:
Peak found.
Waviness:
Number 199
9 is not strictly greater than right neighbor.
No waviness.
Number 201
0 is smaller than both neighbors.
Valley found.
Number 202
Again:
Valley found.
Final Answer
Time Complexity Analysis
Let:
Time Complexity
At most:
which is very small.
Efficient for constraints.
Space Complexity
due to character array conversion.
Why Brute Force Works Here
Constraints are small:
So checking every number directly is acceptable.
For larger constraints:
- Digit DP would be needed.
But here:
- Simplicity is better.
Common Mistakes
1. Including First or Last Digit
These digits cannot be peaks or valleys.
2. Using Non-Strict Comparison
Wrong:
Correct:
because definition says:
3. Forgetting Both Conditions
Need to check:
- Peak
- Valley
Edge Cases
Single Digit Numbers
Waviness:
because fewer than 3 digits.
Repeated Digits
Example:
No peak or valley.
Alternating Digits
Example:
Produces multiple waviness counts.
Interview Explanation
In interviews explain:
Since the constraints are small, we can directly iterate through every number, convert it into digits, and count peaks and valleys by checking neighboring digits.
This demonstrates:
- Observation skills
- Constraint analysis
- Clean implementation
Conclusion
LeetCode 3751 is a clean implementation problem focused on:
- Digit traversal
- Pattern recognition
- Peaks and valleys
- Brute force optimization
The key insight is:
A digit contributes to waviness only if it is strictly greater or strictly smaller than both immediate neighbors.
Once this condition is understood, the implementation becomes very straightforward.





