-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonotonic_array.cpp
44 lines (44 loc) · 1000 Bytes
/
monotonic_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
static const int __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
class Solution {
public:
bool isMonotonic(vector<int> &nums) {
int i, j;
int n = nums.size();
int countinc = 0;
int countdec = 0;
for (i = 1; i < n; i++) {
// for(j=i+1; j<n; j++){
// if(i<=j){
// if(nums[i] < nums[j]){
// countinc++;
// }
// else if(nums[i] > nums[j]){
// countdec++;
// }
// if(nums[i] == nums[j]){
// countinc++;
// countdec++;
// }
// }
// }
if (nums[i] < nums[i - 1]) {
countinc++;
} else if (nums[i] > nums[i - 1]) {
countdec++;
} else {
countinc++;
countdec++;
}
}
if (countinc == n - 1 || countdec == n - 1) {
return true;
} else {
return false;
}
}
};