Longest Mountain in Array LeetCode solution in C++

1
class Solution {
2
public:
3
int longestMountain(vector<int> &arr) {
4
vector<int> v;
5
for (int i = 0; i < arr.size() - 1; i++) {
6
if (arr[i + 1] > arr[i])
7
v.push_back(1);
8
else if (arr[i + 1] < arr[i])
9
v.push_back(-1);
10
else
11
v.push_back(0);
12
}
13
int i = 0;
14
int ans = 0;
15
while (i < v.size()) {
16
int c1 = 0, c2 = 0;
17
while (i < v.size() and v[i] == 1) {
18
c1++;
19
i++;
20
}
21
while (i < v.size() and v[i] == -1) {
22
c2++;
23
i++;
24
}
25
while (i < v.size() and v[i] == 0) {
26
i++;
27
}
28
if (c1 != 0 and c2 != 0) {
29
ans = max(ans, c1 + c2 + 1);
30
}
31
}
32
return ans;
33
}
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0