LeetCode Solutions
896. Monotonic Array
Time: Space:
class Solution {
public:
bool isMonotonic(vector<int>& A) {
bool increasing = true;
bool decreasing = true;
for (int i = 1; i < A.size(); ++i) {
increasing &= A[i] >= A[i - 1];
decreasing &= A[i] <= A[i - 1];
}
return increasing || decreasing;
}
};
class Solution {
public boolean isMonotonic(int[] A) {
boolean increasing = true;
boolean decreasing = true;
for (int i = 1; i < A.length; ++i) {
increasing &= A[i] >= A[i - 1];
decreasing &= A[i] <= A[i - 1];
}
return increasing || decreasing;
}
}
class Solution:
def isMonotonic(self, A: List[int]) -> bool:
increasing = True
decreasing = True
for i in range(1, len(A)):
increasing &= A[i - 1] <= A[i]
decreasing &= A[i - 1] >= A[i]
return increasing or decreasing