2210. Count Hills and Valleys in an Array
Difficulty: Easy
Topics: Array, Weekly Contest 285
You are given a 0-indexed integer array nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger than nums[i]. Adjacent indices i and j are part of the same hill or valley if nums[i] == nums[j].
Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index.
Return the number of hills and valleys in nums.
Example 1:
- Input: nums = [2,4,1,1,6,5]
- Output: 3
-
Explanation:
-
At index 0: There is no non-equal neighbor of
2on the left, so index0is neither a hill nor a valley. -
At index 1: The closest non-equal neighbors of
4are2and1. Since4 > 2and4 > 1, index1is a hill. -
At index 2: The closest non-equal neighbors of
1are4and6. Since1 < 4and1 < 6, index2is a valley. -
At index 3: The closest non-equal neighbors of
1are4and6. Since1 < 4and1 < 6, index3is a valley, but note that it is part of the same valley as index2. -
At index 4: The closest non-equal neighbors of
6are1and5. Since6 > 1and6 > 5, index4is a hill. -
At index 5: There is no non-equal neighbor of
5on the right, so index5is neither a hill nor a valley. - There are
3hills and valleys so we return3.
-
At index 0: There is no non-equal neighbor of
Example 2:
- Input: nums = [6,6,5,5,4,1]
- Output: 0
-
Explanation:
-
At index 0: There is no non-equal neighbor of
6on the left, so index0is neither a hill nor a valley. -
At index 1: There is no non-equal neighbor of
6on the left, so index1is neither a hill nor a valley. -
At index 2: The closest non-equal neighbors of
5are6and4. Since5 < 6and5 > 4, index2is neither a hill nor a valley. -
At index 3: The closest non-equal neighbors of
5are6and4. Since5 < 6and5 > 4, index3is neither a hill nor a valley. -
At index 4: The closest non-equal neighbors of
4are5and1. Since4 < 5and4 > 1, index4is neither a hill nor a valley. -
At index 5: There is no non-equal neighbor of
1on the right, so index5is neither a hill nor a valley. - There are
0hills and valleys so we return0.
-
At index 0: There is no non-equal neighbor of
Constraints:
3 <= nums.length <= 1001 <= nums[i] <= 100
Hint:
- For each index, could you find the closest non-equal neighbors?
- Ensure that adjacent indices that are part of the same hill or valley are not double-counted.
Solution:
We need to count the number of hills and valleys in an array. A hill is an index where the closest non-equal neighbors on both sides are smaller than the current element. Similarly, a valley is an index where the closest non-equal neighbors on both sides are larger than the current element. Adjacent indices with the same value are considered part of the same hill or valley.
Approach
- Preprocess the Array: Remove consecutive duplicates from the array. This step groups adjacent indices with the same value into a single segment, as they are considered part of the same hill or valley.
- Check for Hills and Valleys: Traverse the processed array (excluding the first and last elements since they lack neighbors on one side). For each element, check if it is greater than both neighbors (hill) or smaller than both neighbors (valley).
- Count Valid Hills and Valleys: Increment the count for each valid hill or valley encountered during the traversal.
Let's implement this solution in PHP: 2210. Count Hills and Valleys in an Array
<?php
/**
* @param Integer[] $nums
* @return Integer
*/
function countHillValley($nums) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo countHillValley([2,4,1,1,6,5]); // Output: 3
echo countHillValley([6,6,5,5,4,1]); // Output: 0
?>
Explanation:
-
Preprocessing: The array is processed to remove consecutive duplicates. For example,
[2, 4, 1, 1, 6, 5]becomes[2, 4, 1, 6, 5]. This step ensures that adjacent indices with the same value are treated as a single segment. - Edge Handling: If the processed array has fewer than 3 elements, it's impossible to have both left and right neighbors, so the result is 0.
-
Traversal: The processed array is traversed from the second element to the second-to-last element. For each element, it checks if the element is either:
- Hill: Greater than both adjacent elements.
- Valley: Smaller than both adjacent elements.
- Counting: Each valid hill or valley increments the count. The final count is returned as the result.
This approach efficiently counts hills and valleys by first simplifying the array to avoid redundant checks on consecutive duplicates, then systematically evaluating each relevant element's neighbors. The solution handles all edge cases and processes the array in linear time.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)