码迷,mamicode.com
首页 > 其他好文 > 详细

896. Monotonic Array

时间:2018-09-03 15:33:06      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:turn   targe   app   def   either   pen   info   ==   append   

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

 

896. Monotonic Array

标签:turn   targe   app   def   either   pen   info   ==   append   

原文地址:https://www.cnblogs.com/jyg694234697/p/9578693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!