Count Hills and Valleys in an Array LeetCode solution in C++

1
class Solution {
2
public:
3
int countHillValley(vector<int> &nums) {
4
// taking a new vector
5
vector<int> v;
6
v.push_back(nums[0]);
7
// pushing unique elements into new vector
8
for (int i = 1; i < nums.size(); i++) {
9
if (nums[i] != nums[i - 1]) {
10
v.push_back(nums[i]);
11
}
12
}
13
int c = 0;
14
// checking for valley or hill
15
for (int i = 1; i < v.size() - 1; i++) {
16
if (v[i] > v[i - 1] and v[i] > v[i + 1] or v[i] < v[i - 1] and v[i] < v[i + 1]) {
17
c++;
18
}
19
}
20
return c;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0