896. Monotonic Array
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.
Note:
1 <= A.length <= 50000
-100000 <= A[i] <= 100000
class Solution(object): def isMonotonic(self, A): if (len(A) == 1): return True target_list = [] for i in range(len(A)-1): target_list.append(A[i+1]-A[i]) target_list_stored = sorted(target_list) for j in range(len(target_list_stored)): if target_list_stored[0] <= 0 and target_list_stored[-1] <= 0: return True elif target_list_stored[0] >= 0 and target_list_stored[-1] >= 0: return True else : return False