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.

Example 1:

Input: [1,2,2,3]
Output: true

Example 2:

Input: [6,5,4,4]
Output: true

Example 3:

Input: [1,3,2]
Output: false

Example 4:

Input: [1,2,4,5]
Output: true

Example 5:

Input: [1,1,1]
Output: true

Note:

  1. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000
 
class Solution(object):
def isMonotonic(self, A):
"""
:type A: List[int]
:rtype: bool
"""
n=len(A)
if n==1:
return True
elif A[0]<A[1]:
for i in range(n-1):
if A[i]>A[i+1]:
return False
return True
elif A[0]==A[1]:
ma=max(A)
if ma==A[0]:
for i in range(n-1):
if A[i]<A[i+1]:
return False
return True
else:
for i in range(n-1):
if A[i]>A[i+1]:
return False
return True
else:
for i in range(n-1):
if A[i]<A[i+1]:
return False
return True

  

[LeetCode&Python] Problem 896. Monotonic Array的更多相关文章

  1. [LeetCode&Python] Problem 905: Sort Array By Parity

    Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...

  2. 【Leetcode_easy】896. Monotonic Array

    problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...

  3. 896. Monotonic Array@python

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  4. 【LeetCode】896. Monotonic Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  6. LeetCode 896 Monotonic Array 解题报告

    题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...

  7. LeetCode 896. Monotonic Array

    原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...

  8. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  9. [LeetCode&Python] Problem 697. Degree of an Array

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

随机推荐

  1. weblogic查看版本号教程

    1.查看软件版本号 cd /weblogic/bea/wlserver_10.3/server/lib java -cp weblogic.jar weblogic.version 说明:版本号后边的 ...

  2. Win10系列:JavaScript多媒体

    在应用程序的日常使用中,经常会使用多媒体播放器来播放多媒体文件,包括视频.音频等,因此对于开发者来说,学习多媒体播放技术对开发应用是很有帮助的.本小节主要介绍如何使用HTML5和JavaScrip实现 ...

  3. day05 数据类型

    一.整形int 基本使用: 1,用途:记录年龄\等级\各种号码 2定义方式: age=18     age =int(18) x =int(‘123’)#只能将纯数字的字符串转换成整形 print(t ...

  4. [POJ3378]Crazy Thairs

    Problem 给你一个数列,让你求由五个元素组成的顺序对的个数. Solution DP:用DP[i][j]表示把第j个作为五元组中第i个的方案数 则DP[i][j]=sum{DP[k][j-1]} ...

  5. Java 面向对象的三大特性之一 继承

    继承: Java是继承的三大特性之一,是Java中实现代码重用的手段之一 将重复的代码抽取到父类中继承的有点或者现实 优点: 方便修改代码 减少代码量 Java中继承的语法: 修饰符 SubClass ...

  6. nginx负载均衡实验

    Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...

  7. Linux 目录配置标准:FHS

    目录 应放置内容 /bin 和/user/目录下的/bin/都是用来保存的系统命令 /sbin 和/user/目录下的/sbin是用来保存root的系统命令 /boot 这个目录主要放置开机所用的文件 ...

  8. :适配器模式:Adapter

    #ifndef __ADAPTER_H__ #define __ADAPTER_H__ #include <iostream> using namespace std; class Duc ...

  9. SQL-17 获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary

    题目描述 获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salaryCREATE TABLE `salaries` (`emp_no` int(11 ...

  10. DevExpress ASP.NET v18.2新功能详解(三)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...