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. windows server 2008 r2 x64 enterprise service pack1中aspjpeg.dll安装

    官网地址:http://persits.com/ 64位:http://www.persits.com/aspjpeg64.exe sn:lptn9dQO1enAePAXB2wFaCZawYWzfm0 ...

  2. 在IntelliJ IDEA中使用VIM

    IdeaVim(下载)插件可以让你在IntelliJ IDEA中键盘敲的飞起. 安装 打开IDEA的设置,在Plugins里,你可以选择在线搜索Vim安装,当然如果不行,就可以选择单独下载后安装,以下 ...

  3. [转]检测到有潜在危险的 Request.Form 值

    <system.web> <httpRuntime targetFramework="4.0" requestValidationMode="2.0&q ...

  4. [UE4]Wrap Box流布局

    一.Wrap Box的子控件可以根据Wrap Box的大小自动换行 1.Wrap Box.Inner Slot Padding:Wrap Box所有子控件留白,可以实现每个控件之间的间距都是相同,但是 ...

  5. 1、Shiro 安全框架与Spring 整合详解

    Apache Shiro 是一个安全认证框架,和 Spring Security 相比,在于他使用了比较简洁易懂的认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存在 ...

  6. 八(第二篇)、主体结构元素——nav元素、aside元素

    nav元素 nav元素是一个可以用作页面导航的链接组,其中的导航元素链接到其他页面或当前页面的其他部分. 并不是所有的链接组都要被放进nav元素,只需要将主要的.基本的链接组放进nav元素即可. na ...

  7. SpringBoot 之热部署

    默认情况下, 我们修改 class 或者 修改模板文件(templates目录 下面的文件) 等动态资源, 都不会立即自动生效. 在IDEA中, 我通过Ctrl + F9 , 仍然是无效. 当然, 静 ...

  8. leetcode56

    public class Solution { public IList<Interval> Merge(IList<Interval> intervals) { var le ...

  9. leetcode300

    本题使用回溯法,深度优先搜索.使用隐式条件来进行加速. public class Solution { ; int[] x; Dictionary<int, int> dic = new ...

  10. python 数据分析库介绍

    1 引言 高效处理数据的python工具: 与外界进行交互: 读写各种文件格式和数据库 准备: 对数据进行清理.修整.整合.规范化.重塑.切片切换.变形等处理以便进行分析 转换: 对数据集做一些数学和 ...