[LeetCode] 2210. Count Hills and Valleys in an Array

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 2 on the left, so index 0 is neither a hill nor a valley.
At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill.
At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.
At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.
At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley.
There are 3 hills and valleys so we return 3.

Example 2:
Input: nums = [6,6,5,5,4,1]
Output: 0
Explanation:
At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.
At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.
At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.
At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
There are 0 hills and valleys so we return 0.

Constraints:
3 <= nums.length <= 100
1 <= nums[i] <= 100

统计数组中峰和谷的数量。

给你一个下标从 0 开始的整数数组 nums 。如果两侧距 i 最近的不相等邻居的值均小于 nums[i] ,则下标 i 是 nums 中,某个峰的一部分。类似地,如果两侧距 i 最近的不相等邻居的值均大于 nums[i] ,则下标 i 是 nums 中某个谷的一部分。对于相邻下标 i 和 j ,如果 nums[i] == nums[j] , 则认为这两下标属于 同一个 峰或谷。

注意,要使某个下标所做峰或谷的一部分,那么它左右两侧必须 都 存在不相等邻居。

返回 nums 中峰和谷的数量。

思路一 - 去掉重复元素

类似26题那样,先去掉重复元素,然后再统计峰和谷。

复杂度

时间O(n)
空间O(1)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int countHillValley(int[] nums) {
int n = nums.length;
int j = 1;
for (int i = 1; i < n; i++) {
if (nums[i] != nums[j - 1]) {
nums[j] = nums[i];
j++;
}
}

int count = 0;
for (int i = 1; i < j - 1; i++) {
if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
count++;
}
if (nums[i] < nums[i - 1] && nums[i] < nums[i + 1]) {
count++;
}
}
return count;
}
}
// similar to 26

思路二 - 状态机

还是判断每两个相邻元素的大小关系,并用一个变量记录上一对元素比较的结果。如果当前两个元素的比较结果和上一个比较结果不同,则说明当前元素是峰或谷。

复杂度

时间O(n)
空间O(1)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int countHillValley(int[] nums) {
int res = 0;
int prestate = 0;
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
int cur = nums[i];
int next = nums[i + 1];
if (cur > next) {
if (prestate == 1) {
res++;
}
prestate = -1;
} else if (cur < next) {
if (prestate == -1) {
res++;
}
prestate = 1;
}
}
return res;
}
}

[LeetCode] 2210. Count Hills and Valleys in an Array
https://shurui91.github.io/posts/515074821.html
Author
Aaron Liu
Posted on
July 26, 2025
Licensed under