Count how many indices in an array are local hills or valleys after ignoring consecutive duplicates.
Problem
You are given an integer array nums. A position is considered a hill if its value is strictly greater than the nearest different elements on both sides. A position is considered a valley if its value is strictly smaller than the nearest different elements on both sides.
Consecutive equal values should be treated as a single flat region when checking whether a position is a hill or valley.
Return the number of hills and valleys in the array.
Notes
- Only values with a valid different element on both the left and right can be counted.
- The answer counts each hill or valley once, based on the distinct local shape around that position.
- Adjacent equal values do not create additional hills or valleys by themselves.
Input Format
- An integer array
nums.
Each element represents the height/value at that position.
Output Format
- Return an integer: the number of hills and valleys in
nums.
Constraints
1 <= nums.length\n- Array values are integers\n- The count is based on local shape after ignoring consecutive duplicates
Example 1
Input
nums = [2,4,1,1,6,5]
Output
3
Explanation
After ignoring the repeated 1, the distinct sequence is [2,4,1,6,5]. The value 4 is a hill, 1 is a valley, and 6 is a hill, so the answer is 3.
Example 2
Input
nums = [6,6,5,5,4,1]
Output
0
Explanation
The sequence never forms a strict local peak or dip once consecutive duplicates are ignored.
Show 1 more example
Example 3
Input
nums = [1,2,2,2,1]
Output
0
Explanation
The middle flat region does not create a hill or valley.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.