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 <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[i] >= A[j].

Return true if and only if the given array A is monotonic.

 

Note:

  1. 1 <= A.length <= 50000
  2. -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

 

posted on 2018-09-03 15:16  jydd  阅读(97)  评论(0编辑  收藏  举报

导航