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. 被称为“开发者神器”的GitHub,到底该怎么用?

    被称为“开发者神器”的GitHub,到底该怎么用? 原文:https://baijiahao.baidu.com/s?id=1594232691312740966&wfr=spider& ...

  2. java中如何给控件设置颜色

     1. tv.setTextColor(Color.parseColor("#000000"));2. tv.setTextColor(getResources().getCo ...

  3. 高性能mysql 第六章查询性能优化 总结(上)查询的执行过程

    6  查询性能优化 6.1为什么查询会变慢 这里说明了的查询执行周期,从客户端到服务器端,服务器端解析,优化器生成执行计划,执行(可以细分,大体过程可以通过show profile查看),从服务器端返 ...

  4. 关于tp5自动过滤index.php

    在public/.htaccess 中输入这段代码即可实现过滤index.php <IfModule mod_rewrite.c> Options +FollowSymlinks -Mul ...

  5. oracle 序列sequence

    查询所有的序列: select 'create sequence '||sequence_name|| ' minvalue '||min_value|| ' maxvalue '||max_valu ...

  6. CUDA compiler driver nvcc 散点 part 1

    ▶ 参考[https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html] ▶ nvcc 预定义的宏 __NVCC__ // 编译 ...

  7. gdb 使用

    2018年7月27日21:05:16 —— 多进程调试 1.follow_fork_mode 作用:在fork之后跟随父进程还是子进程 可以使用 show follow_fork_mode查看再for ...

  8. SQLite在Android程序中的使用方法,SQLite的增删查改方法

    Sqlite: 1.一款用来实现本地数据存储的轻量级数据管理工具,是众多用来实现数据库管理的工具之一. 2.Android已经将SQLite的代码功能吸收在它的系统中,我们可以直接在Android程序 ...

  9. python入门学习0

    Python 是什么类型的语言 Python是脚本语言 Python下载地址:https://www.python.org/downloads/ Python版本:Python 3.4.2 - 64b ...

  10. Session & Cookie 简介

    (一)简介 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通 ...