-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
36 lines (33 loc) · 950 Bytes
/
main.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
class Solution {
public:
bool isMonotonic(vector<int>& nums) {
auto increasing = [&]() -> bool {
for (int i = 0; i < nums.size() - 1; ++i) {
if (nums[i] <= nums[i + 1]) continue;
return false;
}
return true;
};
auto decreasing = [&]() -> bool {
for (int i = 0; i < nums.size() - 1; ++i) {
if (nums[i] >= nums[i + 1]) continue;
return false;
}
return true;
};
return increasing() || decreasing();
}
};
class Solution {
public:
bool isMonotonic(vector<int>& nums) {
bool inc = true, dec = true;
int mn = numeric_limits<int>::max(), mx = numeric_limits<int>::min();
for (int v : nums) {
if (v < mx) inc = false;
if (v > mn) dec = false;
mn = mx = v;
}
return inc || dec;
}
};