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. <realsense D400>同步采集深度图和彩色图

    利用深度相机采集深度图和彩色图会面临一个问题,如何实现同步采集数据? 以下是我搜集到的两点方法: 1)高翔博士提到他的orbslam2教程有这么一步工作,具体目录为 example/RGBD/. (等 ...

  2. 学Python的原因

    先立个旗,不学会誓不为人!!!!!!!!!!! 一直以来总是三天打鱼,两天晒网的学习,但是在体制内混久了发现,失去了很多的东西,得到的确极其有限,总感觉这样的生活会失去意义. 寻找生活的激情,重新发现 ...

  3. [UE4]UMG小结

    一.当没有需要的UI怎么办?先别急着自己定制,可以到虚幻商城去看看,各种类型的UI都有,而且价格都不贵. 二.推荐一个比较有参考价值的UI:User Interface Kit,里面的UI很多,还有小 ...

  4. mysql相关碎碎念

    取得当天: SELECT curdate(); mysql> SELECT curdate();+------------+| curdate()  |+------------+| 2013- ...

  5. [UnityAPI]SerializedObject类 & SerializedProperty类

    以Image类为例 1.MyImage.cs using UnityEngine; using UnityEngine.UI; public class MyImage : Image { ; pro ...

  6. 面试回顾——List<T>排序

    1.如何对List<T>排序: public static void main(String[] args) { Student stu1=new Student("张三&quo ...

  7. Spring利用注解@Value获取properties属性为null

    今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...

  8. jeecg开源项目的IDEA的部署

    JEECG采用了SpringMVC + Hibernate + Minidao(类Mybatis) + Easyui(UI库)+ Jquery + Boostrap + Ehcache + Redis ...

  9. Jquery DataTables 获取表格数据

    1.获取表格所有数据 function getTableContent(){ var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行,nTrs[i]表示第i ...

  10. 2018面向对象程序设计(Java)第17周学习指导及要求

    2018面向对象程序设计(Java)第17周学习指导及要求(2018.12.20-2018.12.23)   学习目标 (1) 掌握线程同步的概念及实现技术: (2) Java线程综合编程练习 学习资 ...