Skip to content

Commit 8b4b395

Browse files
committed
Add C++ solution of problem #3105
1 parent a7c361a commit 8b4b395

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

3105/solution.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int longestMonotonicSubarray(vector<int>& A) {
4+
int in = 0, de = 0; // max len of strictly increasing/decreasing
5+
int tmp = 0; // previous element
6+
int ans = 1;
7+
for(int i = 0; i < A.size(); i++) {
8+
if(i == 0) {
9+
in = de = 1;
10+
tmp = A[i];
11+
continue;
12+
}
13+
14+
// increasing
15+
if(A[i] > tmp)
16+
in++;
17+
else
18+
in = 1;
19+
20+
// decreasing
21+
if(A[i] < tmp)
22+
de++;
23+
else
24+
de = 1;
25+
26+
tmp = A[i];
27+
ans = max({ans, in, de});
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)