-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17-longest-mountain-in-array.cpp
54 lines (51 loc) · 1.07 KB
/
17-longest-mountain-in-array.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int longestMountain(vector<int>& arr) {
int ans = 0, count = 0;
int n = arr.size(), i = 1;
while(i < n)
{
int flag = 0;
while(i < n && arr[i] > arr[i - 1])
{
count++;
flag = 1;
i++;
}
if(flag == 1)
{
while(i < n && arr[i] < arr[i - 1])
{
count++;
flag = 2;
i++;
}
}
if(flag == 2)
{
count += 1;
ans = max(ans, count);
count = 0;
}
else
{
i++;
count = 0;
}
}
return ans;
}
};
int main()
{
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; ++i)
cin >> arr[i];
Solution ob;
cout << ob.longestMountain(arr) << endl;
}