896 - Monotonic Array
Written on February 7, 2020
Tweet
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if the given array A is monotonic.
class Solution:
def isMonotonic(self, A: List[int]) -> bool:
if not A:
return True
increase = decrease = False
for i in range(1, len(A)):
if A[i] < A[i - 1]:
decrease = True
elif A[i] > A[i - 1]:
increase = True
return False if decrease and increase else True