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.

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

Idea 1. Check if it is monotonic increasing or decreasing, 需要2个pass, 自己曾想把这2个合二为一个pass, 才意识到有==, 序列开始可以是递增或递减

Time complexity: O(n), 2 scan

Space complexity: O(1)

 class Solution {
private boolean isMonotonicIncreasing(int[] A) {
int i = 1;
while(i < A.length && A[i-1] <= A[i]) {
++i;
}
return i == A.length;
} private boolean isMonotonicDecreasing(int[] A) {
int i = 1;
while(i < A.length && A[i-1] >= A[i]) {
++i;
}
return i == A.length;
} public boolean isMonotonic(int[] A) {
return isMonotonicIncreasing(A) || isMonotonicDecreasing(A);
}
}

反着想,有递增pair就不是递减

 class Solution {
private boolean isMonotonicIncreasing(int[] A) {
for(int i = 1; i < A.length; ++i) {
if(A[i-1] > A[i]) {
return false;
}
}
return true;
} private boolean isMonotonicDecreasing(int[] A) {
for(int i = 1; i < A.length; ++i) {
if(A[i-1] < A[i]) {
return false;
}
}
return true;
} public boolean isMonotonic(int[] A) {
return isMonotonicIncreasing(A) || isMonotonicDecreasing(A);
}
}

Idea 1.b. Similar to Longest Turbulent Subarray LT978, store the sign and return false if current sign is opposite to previous sign.

Time complexity: O(n)

Space complexity: O(1)

 class Solution {

     public boolean isMonotonic(int[] A) {
int flag = 0;
for(int i = 1; i < A.length; ++i) {
int c = Integer.compare(A[i-1], A[i]);
if(c == 0) {
continue;
} if(flag != 0 && c != flag) {
return false;
}
flag = c;
} return true;
}
}

Idea 1.c 如果同时有decreasing and increasing pair, 肯定不是monotonic

 class Solution {
public boolean isMonotonic(int[] A) {
boolean increasing = false;
boolean decreasing = false;
for(int i = 1; i < A.length; ++i) {
if(A[i-1] > A[i]) {
increasing = true;
}
else if(A[i-1] < A[i]) {
decreasing = true;
}
} if(increasing && decreasing) {
return false;
} return true;
}
}

官方的妙法,反着来, 注意上面的解法直接return increasing^decreasing不行,考虑数组全是==, 官方的这个考虑到了,就是想法有些绕

 class Solution {
public boolean isMonotonic(int[] A) {
boolean increasing = true;
boolean decreasing = true;
for(int i = 1; i < A.length; ++i) {
if(A[i-1] < A[i]) {
increasing = false;
}
else if(A[i-1] > A[i]) {
decreasing = false;
}
} return increasing || decreasing;
}
}

Monotonic Array LT896的更多相关文章

  1. 896. Monotonic Array@python

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

  2. 【Leetcode_easy】896. Monotonic Array

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

  3. LeetCode 896. 单调数列(Monotonic Array)

    896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...

  4. [Swift]LeetCode896. 单调数列 | Monotonic Array

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

  5. LeetCode 896 Monotonic Array 解题报告

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

  6. [LeetCode&Python] Problem 896. Monotonic Array

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

  7. 896. Monotonic Array单调数组

    [抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...

  8. 896. Monotonic Array

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

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

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

随机推荐

  1. JAVA AES CBC 加密 解密

    AES 256 , KEY 的长度为 32字节(32*8=256bit). AES 128 , KEY 的长度为 16字节(16*8=128bit) CBC 模式需要IV, IV的值是固定写死,还是当 ...

  2. redis高可用(哨兵机制)

    redis哨兵机制:redis的哨兵系统用于管理多个reids服务器,该系统主要有三个作用: 监控:哨兵 会不断地检查你的主服务(Master)和从服务器(Slave)是否运作正常. 提醒:当被监控的 ...

  3. sitemesh使用

    参考文章: https://my.oschina.net/heroShane/blog/199177 http://blog.csdn.net/u013019926/article/details/1 ...

  4. 从Tomcat的处理web请求分析Java的内存模型

    Tomcat作为一个java应用,同样是有主线程和子线程的.主线使用while(true)的方式一直循环,等待客户端来连接.一个客户端来了之后,就从线程池中拿一个线程来处理请求,如果没有配置线程池,就 ...

  5. Northwind数据库练习及参考答案

    --查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期.订单ID.客户ID和雇员ID等字段的值 Create View Orderquery as Select OrderDa ...

  6. Java笔试面试题整理第三波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  7. 转:StarUML3.0的破解方法

    转自:https://blog.csdn.net/sam_shan/article/details/80585240 StarUML3.0的破解方法 最近StarUML由2.0更新到3.0.原来的破解 ...

  8. MPP数据库

    MPP数据库   版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lyc417356935/article/details/45033069 MPP数据库定 ...

  9. py库:把python打包成exe文件(pyinstaller)

    http://blog.csdn.net/be_quiet_endeavor/article/details/73929077 用Pyinstaller把Python3.4程序打包成可执行文件exe ...

  10. 06python上

    product_list=[ ('Mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike',2000), ] ...