-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3105.cpp
More file actions
35 lines (33 loc) · 719 Bytes
/
3105.cpp
File metadata and controls
35 lines (33 loc) · 719 Bytes
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
class Solution {
public:
int longestMonotonicSubarray(vector<int>& nums) {
int res = 1;
int cnt = 0;
// increase
int curr = INT_MIN;
for (auto& num : nums) {
if (num > curr) {
cnt++;
res = max(res, cnt);
}
else {
cnt = 1;
}
curr = num;
}
// decrease
curr = INT_MAX;
cnt = 0;
for (auto& num : nums) {
if (num < curr) {
cnt++;
res = max(res, cnt);
}
else {
cnt = 1;
}
curr = num;
}
return res;
}
};