Skip to content

Commit 3b5fce1

Browse files
LetMeFly666Copilot
andauthored
update: 添加问题“2210.统计数组中峰和谷的数量”的代码和题解(#1050)
* 2210: WA.cpp (#1047) * 2210: WA.cpp (#1047) * 2210: WA.cpp (#1047) * 2210: AC.cpp (#1047) cpp - AC,100.00%,92.38% * 2210: WA.py (#1047) * 2210: AC.py (#1047) - remarkable - 值得记录的错误修复 py - AC,100.00%,78.02% * 2210: AC.java+go (#1047) java - AC,100.00%,26.13% go - AC,100.00%,38.98% * update: 添加问题“2210.统计数组中峰和谷的数量”的代码和题解(#1050) Signed-off-by: LetMeFly666 <[email protected]> * docs: cpp20写法 (#1049) (#1050) - 越来越不像cpp了 * typo: Update Solutions/LeetCode 2210.统计数组中峰和谷的数量.md Co-authored-by: Copilot <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 0981691 commit 3b5fce1

7 files changed

+284
-313
lines changed

.gitlog

Lines changed: 0 additions & 313 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-28 21:39:52
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-28 21:45:22
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countHillValley(vector<int>& nums) {
14+
int ans = 0;
15+
int last = nums[0];
16+
for (int i = 1; i < nums.size() - 1; i++) {
17+
if (nums[i] > last && nums[i] > nums[i + 1]) {
18+
ans++;
19+
} else if (nums[i] < last && nums[i] < nums[i + 1]) {
20+
ans++;
21+
}
22+
if (nums[i] != nums[i + 1]) {
23+
last = nums[i];
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-28 21:39:52
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-28 22:27:50
6+
*/
7+
package main
8+
9+
func countHillValley(nums []int) (ans int) {
10+
last := nums[0]
11+
for i := 1; i < len(nums) - 1; i++ {
12+
if nums[i] > last && nums[i] > nums[i + 1] || nums[i] < last && nums[i] < nums[i + 1] {
13+
ans++
14+
}
15+
if nums[i] != nums[i + 1] {
16+
last = nums[i]
17+
}
18+
}
19+
return
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-28 21:39:52
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-28 22:25:46
6+
*/
7+
class Solution {
8+
public int countHillValley(int[] nums) {
9+
int last = nums[0];
10+
int ans = 0;
11+
for (int i = 1; i < nums.length - 1; i++) {
12+
if (nums[i] > last && nums[i] > nums[i + 1] || nums[i] < last && nums[i] < nums[i + 1]) {
13+
ans++;
14+
}
15+
if (nums[i] != nums[i + 1]) {
16+
last = nums[i];
17+
}
18+
}
19+
return ans;
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-07-28 21:39:52
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-07-28 22:23:33
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countHillValley(self, nums: List[int]) -> int:
11+
last = nums[0]
12+
ans = 0
13+
# for i, t in enumerate(nums[1:-1]): # 不可!这样i=0时t=nums[1]
14+
for i in range(1, len(nums) - 1):
15+
ans += nums[i] > last and nums[i] > nums[i + 1] or nums[i] < last and nums[i] < nums[i + 1]
16+
if nums[i] != nums[i + 1]:
17+
last = nums[i]
18+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@
699699
|2207.字符串中最多数目的子序列|中等|<a href="https://leetcode.cn/problems/maximize-number-of-subsequences-in-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/24/LeetCode%202207.%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%9C%80%E5%A4%9A%E6%95%B0%E7%9B%AE%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142497503" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximize-number-of-subsequences-in-a-string/solutions/2929022/letmefly-2207zi-fu-chuan-zhong-zui-duo-s-49nr/" target="_blank">LeetCode题解</a>|
700700
|2208.将数组和减半的最少操作次数|中等|<a href="https://leetcode.cn/problems/minimum-operations-to-halve-array-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/25/LeetCode%202208.%E5%B0%86%E6%95%B0%E7%BB%84%E5%92%8C%E5%87%8F%E5%8D%8A%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131908685" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-halve-array-sum/solutions/2357632/letmefly-2208jiang-shu-zu-he-jian-ban-de-5gl0/" target="_blank">LeetCode题解</a>|
701701
|2209.用地毯覆盖后的最少白色砖块|困难|<a href="https://leetcode.cn/problems/minimum-white-tiles-after-covering-with-carpets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/21/LeetCode%202209.%E7%94%A8%E5%9C%B0%E6%AF%AF%E8%A6%86%E7%9B%96%E5%90%8E%E7%9A%84%E6%9C%80%E5%B0%91%E7%99%BD%E8%89%B2%E7%A0%96%E5%9D%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145777611" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-white-tiles-after-covering-with-carpets/solutions/3083946/letmefly-ji-yi-hua-sou-suo-zhi-shen-du-y-0gem/" target="_blank">LeetCode题解</a>|
702+
|2210.统计数组中峰和谷的数量|简单|<a href="https://leetcode.cn/problems/count-hills-and-valleys-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/28/LeetCode%202210.%E7%BB%9F%E8%AE%A1%E6%95%B0%E7%BB%84%E4%B8%AD%E5%B3%B0%E5%92%8C%E8%B0%B7%E7%9A%84%E6%95%B0%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149727208" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-hills-and-valleys-in-an-array/solutions/3736136/letmefly-2210tong-ji-shu-zu-zhong-feng-h-kxve/" target="_blank">LeetCode题解</a>|
702703
|2225.找出输掉零场或一场比赛的玩家|中等|<a href="https://leetcode.cn/problems/find-players-with-zero-or-one-losses/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/22/LeetCode%202225.%E6%89%BE%E5%87%BA%E8%BE%93%E6%8E%89%E9%9B%B6%E5%9C%BA%E6%88%96%E4%B8%80%E5%9C%BA%E6%AF%94%E8%B5%9B%E7%9A%84%E7%8E%A9%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139128938" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-players-with-zero-or-one-losses/solutions/2786820/letmefly-2225zhao-chu-shu-diao-ling-chan-1bq6/" target="_blank">LeetCode题解</a>|
703704
|2235.两整数相加|简单|<a href="https://leetcode.cn/problems/add-two-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/19/LeetCode%202235.%E4%B8%A4%E6%95%B4%E6%95%B0%E7%9B%B8%E5%8A%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132376238" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/add-two-integers/solutions/2394963/2235liang-zheng-shu-xiang-jia-19chong-yu-42vp/" target="_blank">LeetCode题解</a>|
704705
|2236.判断根结点是否等于子结点之和|简单|<a href="https://leetcode.cn/problems/root-equals-sum-of-children/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/20/LeetCode%202236.%E5%88%A4%E6%96%AD%E6%A0%B9%E7%BB%93%E7%82%B9%E6%98%AF%E5%90%A6%E7%AD%89%E4%BA%8E%E5%AD%90%E7%BB%93%E7%82%B9%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132388754" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/root-equals-sum-of-children/solutions/2396156/letmefly-2236pan-duan-gen-jie-dian-shi-f-gfpd/" target="_blank">LeetCode题解</a>|
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
---
2+
title: 2210.统计数组中峰和谷的数量:一次遍历(记录上一个不同的数)
3+
date: 2025-07-28 22:28:53
4+
tags: [题解, LeetCode, 简单, 数组, 遍历, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2210.统计数组中峰和谷的数量:一次遍历(记录上一个不同的数)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-hills-and-valleys-in-an-array/](https://leetcode.cn/problems/count-hills-and-valleys-in-an-array/)
11+
12+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。如果两侧距 <code>i</code> 最近的不相等邻居的值均小于 <code>nums[i]</code> ,则下标 <code>i</code> 是 <code>nums</code> 中,某个峰的一部分。类似地,如果两侧距 <code>i</code> 最近的不相等邻居的值均大于 <code>nums[i]</code> ,则下标 <code>i</code> 是 <code>nums</code> 中某个谷的一部分。对于相邻下标&nbsp;<code>i</code> 和 <code>j</code> ,如果&nbsp;<code>nums[i] == nums[j]</code> , 则认为这两下标属于 <strong>同一个</strong> 峰或谷。</p>
13+
14+
<p>注意,要使某个下标所在峰或谷的一部分,那么它左右两侧必须 <strong>都</strong> 存在不相等邻居。</p>
15+
16+
<p>返回 <code>nums</code> 中峰和谷的数量。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>nums = [2,4,1,1,6,5]
24+
<strong>输出:</strong>3
25+
<strong>解释:</strong>
26+
在下标 0 :由于 2 的左侧不存在不相等邻居,所以下标 0 既不是峰也不是谷。
27+
在下标 1 :4 的最近不相等邻居是 2 和 1 。由于 4 &gt; 2 且 4 &gt; 1 ,下标 1 是一个峰。
28+
在下标 2 :1 的最近不相等邻居是 4 和 6 。由于 1 &lt; 4 且 1 &lt; 6 ,下标 2 是一个谷。
29+
在下标 3 :1 的最近不相等邻居是 4 和 6 。由于 1 &lt; 4 且 1 &lt; 6 ,下标 3 符合谷的定义,但需要注意它和下标 2 是同一个谷的一部分。
30+
在下标 4 :6 的最近不相等邻居是 1 和 5 。由于 6 &gt; 1 且 6 &gt; 5 ,下标 4 是一个峰。
31+
在下标 5 :由于 5 的右侧不存在不相等邻居,所以下标 5 既不是峰也不是谷。
32+
共有 3 个峰和谷,所以返回 3 。</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<pre>
37+
<strong>输入:</strong>nums = [6,6,5,5,4,1]
38+
<strong>输出:</strong>0
39+
<strong>解释:</strong>
40+
在下标 0 :由于 6 的左侧不存在不相等邻居,所以下标 0 既不是峰也不是谷。
41+
在下标 1 :由于 6 的左侧不存在不相等邻居,所以下标 1 既不是峰也不是谷。
42+
在下标 2 :5 的最近不相等邻居是 6 和 4 。由于 5 &lt; 6 且 5 &gt; 4 ,下标 2 既不是峰也不是谷。
43+
在下标 3 :5 的最近不相等邻居是 6 和 4 。由于 5 &lt; 6 且 5 &gt; 4 ,下标 3 既不是峰也不是谷。
44+
在下标 4 :4 的最近不相等邻居是 5 和 1 。由于 4 &lt; 5 且 4 &gt; 1 ,下标 4 既不是峰也不是谷。
45+
在下标 5 :由于 1 的右侧不存在不相等邻居,所以下标 5 既不是峰也不是谷。
46+
共有 0 个峰和谷,所以返回 0 。
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
55+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
56+
</ul>
57+
58+
59+
60+
## 解题方法:一次遍历
61+
62+
使用last记录上一个和当前元素不相同的元素,从第一个元素到倒数第二个元素遍历nums数组,如果当前元素比last和下一个元素都大或都小,则答案数量加一。如果当前元素和下一个元素不相同,则更新last为当前元素。
63+
64+
特别的,我们可以将last的初始值设置为nums[0],这样当nums[1]和nums[0]相等时,last的值其实和含义“上一个不同元素”不匹配,但并不影响结果的判断。
65+
66+
+ 时间复杂度$O(len(nums))$
67+
+ 空间复杂度$O(1)$
68+
69+
### AC代码
70+
71+
#### C++
72+
73+
```cpp
74+
/*
75+
* @Author: LetMeFly
76+
* @Date: 2025-07-28 21:39:52
77+
* @LastEditors: LetMeFly.xyz
78+
* @LastEditTime: 2025-07-28 21:45:22
79+
*/
80+
#if defined(_WIN32) || defined(__APPLE__)
81+
#include "_[1,2]toVector.h"
82+
#endif
83+
84+
class Solution {
85+
public:
86+
int countHillValley(vector<int>& nums) {
87+
int ans = 0;
88+
int last = nums[0];
89+
for (int i = 1; i < nums.size() - 1; i++) {
90+
if (nums[i] > last && nums[i] > nums[i + 1]) {
91+
ans++;
92+
} else if (nums[i] < last && nums[i] < nums[i + 1]) {
93+
ans++;
94+
}
95+
if (nums[i] != nums[i + 1]) {
96+
last = nums[i];
97+
}
98+
}
99+
return ans;
100+
}
101+
};
102+
```
103+
104+
贴一个[来自扣友](https://leetcode.cn/problems/count-hills-and-valleys-in-an-array/solutions/1374467/tong-ji-shu-zu-zhong-feng-he-gu-de-shu-l-ca7e/comments/3085283/)的C++20高级写法:
105+
106+
```cpp
107+
#include <ranges>
108+
class Solution {
109+
public:
110+
int countHillValley(vector<int>& nums) {
111+
return ranges::count(nums
112+
| views::chunk_by(equal_to{})
113+
| views::transform(ranges::begin)
114+
| views::adjacent_transform<3>([](auto a, auto b, auto c) { return (*a <=> *b) == (*c <=> *b); })
115+
, true);
116+
}
117+
};
118+
```
119+
120+
#### Python
121+
122+
```python
123+
'''
124+
Author: LetMeFly
125+
Date: 2025-07-28 21:39:52
126+
LastEditors: LetMeFly.xyz
127+
LastEditTime: 2025-07-28 22:23:33
128+
'''
129+
from typing import List
130+
131+
class Solution:
132+
def countHillValley(self, nums: List[int]) -> int:
133+
last = nums[0]
134+
ans = 0
135+
# for i, t in enumerate(nums[1:-1]): # 不可!这样i=0时t=nums[1]
136+
for i in range(1, len(nums) - 1):
137+
ans += nums[i] > last and nums[i] > nums[i + 1] or nums[i] < last and nums[i] < nums[i + 1]
138+
if nums[i] != nums[i + 1]:
139+
last = nums[i]
140+
return ans
141+
```
142+
143+
#### Java
144+
145+
```java
146+
/*
147+
* @Author: LetMeFly
148+
* @Date: 2025-07-28 21:39:52
149+
* @LastEditors: LetMeFly.xyz
150+
* @LastEditTime: 2025-07-28 22:25:46
151+
*/
152+
class Solution {
153+
public int countHillValley(int[] nums) {
154+
int last = nums[0];
155+
int ans = 0;
156+
for (int i = 1; i < nums.length - 1; i++) {
157+
if (nums[i] > last && nums[i] > nums[i + 1] || nums[i] < last && nums[i] < nums[i + 1]) {
158+
ans++;
159+
}
160+
if (nums[i] != nums[i + 1]) {
161+
last = nums[i];
162+
}
163+
}
164+
return ans;
165+
}
166+
}
167+
```
168+
169+
#### Go
170+
171+
```go
172+
/*
173+
* @Author: LetMeFly
174+
* @Date: 2025-07-28 21:39:52
175+
* @LastEditors: LetMeFly.xyz
176+
* @LastEditTime: 2025-07-28 22:27:50
177+
*/
178+
package main
179+
180+
func countHillValley(nums []int) (ans int) {
181+
last := nums[0]
182+
for i := 1; i < len(nums) - 1; i++ {
183+
if nums[i] > last && nums[i] > nums[i + 1] || nums[i] < last && nums[i] < nums[i + 1] {
184+
ans++
185+
}
186+
if nums[i] != nums[i + 1] {
187+
last = nums[i]
188+
}
189+
}
190+
return
191+
}
192+
```
193+
194+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/149727208)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/07/28/LeetCode%202210.%E7%BB%9F%E8%AE%A1%E6%95%B0%E7%BB%84%E4%B8%AD%E5%B3%B0%E5%92%8C%E8%B0%B7%E7%9A%84%E6%95%B0%E9%87%8F/)哦~
195+
>
196+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)