-
Notifications
You must be signed in to change notification settings - Fork 0
/
896.Monotonic_Array.cpp
29 lines (22 loc) · 1009 Bytes
/
896.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
class Solution {
public:
bool isMonotonic(vector<int>& nums) {
bool increasing = true;
bool decreasing = true;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > nums[i - 1]) {
decreasing = false;
} else if (nums[i] < nums[i - 1]) {
increasing = false;
}
// If neither increasing nor decreasing, the array is not monotonic
if (!increasing && !decreasing) {
return false;
}
}
return true;
}
};
// basically it checks at a time if the current element is greater than the last element if it is we update the decreasing variable to false (if the it is false then the array is not monotonic decresing)
// Else we check if the current element is less than the previous element then we update the increasing bool to false(if it is fale then it means that the array is not monotonic increasing)
// Then we check for the both if both are false we return false else we return true