Skip to content

Commit 422b908

Browse files
authored
Create 845. Longest Mountain in Array.cpp
1 parent 092d58e commit 422b908

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int longestMountain(vector<int>& arr) {
4+
int l = 0, r = 0, res = 0;
5+
6+
while(r < arr.size()) {
7+
bool ok1 = false;
8+
bool ok2 = false;
9+
10+
while(r < arr.size() - 1 && arr[r] < arr[r + 1]) {
11+
ok1 = true;
12+
++r;
13+
}
14+
15+
while(r < arr.size() - 1 && arr[r] > arr[r + 1]) {
16+
ok2 = true;
17+
++r;
18+
}
19+
20+
if(ok1 && ok2) {
21+
res = max(res, r - l + 1);
22+
} else if(!ok1 && !ok2) {
23+
++r;
24+
}
25+
26+
l = r;
27+
}
28+
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)