-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_2210.cpp
More file actions
39 lines (30 loc) · 859 Bytes
/
Copy pathLeetCode_2210.cpp
File metadata and controls
39 lines (30 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Problem Count Hills and valleys
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int countHillValley(vector<int>& nums) {
int count = 0;
int i = 1;
while (i < nums.size() - 1) {
if (nums[i] == nums[i - 1]) {
i++;
continue;
}
int right = i + 1;
while (right < nums.size() && nums[right] == nums[i]) {
right++;
}
if (right < nums.size()) {
if (nums[i] > nums[i - 1] && nums[i] > nums[right]) {
count++;
} else if (nums[i] < nums[i - 1] && nums[i] < nums[right]) {
count++;
}
}
i++;
}
return count;
}
};