896. Monotonic Array - Easy

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

M1: two pass

一开始试图在one pass判断两种单调状态,但无法处理 {1, 1, 0}这样的情况,必须得two pass分别判断是单调增还是单调减,再把结果取或。需要遍历两次数组,不是最优。

时间:O(N),空间:O(1)

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

M2: one pass

在M1的基础上,用两个boolean变量表示增还是减。先把两个值都初始化为true。如果有元素递减,increase=false;如果有元素递增,decrease=false。最后返回increase或decrease

时间:O(N),空间:O(1)

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

原文地址:https://www.cnblogs.com/fatttcat/p/10053530.html

时间: 2024-10-15 03:53:50

896. Monotonic Array - Easy的相关文章

[LeetCode] 896. Monotonic Array 单调数组

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

896. Monotonic Array

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

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

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

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

leetcode_26_ Remove Duplicates from Sorted Array (easy)

Remove Duplicates from Sorted Array 题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem

leetcodeRemove Duplicates from Sorted Array(easy) /java

我决定先刷easy. 这道题的诡异之处在于,不仅你得输出长度,你还得更改nums[]数组,把冗余的数清掉.比如 import java.io.*; import java.util.*; public class Solution { public static int removeDuplicates(int[] nums) { int r=0; int len=nums.length; int i=0,j=0; if(len<2) return 0; int cmp=nums[0]; int

Leetcode——array EASY笔记

448. Find All Numbers Disappeared in an Array 误区,O(n)不是要求只能有一个for循环,而是要求,不能有嵌套的for循环!!! 所以可以一个for结束,再一个for结束····这样的 气死了!!数组是 i=0;i<nums.length;i++; 要减一就<=!!!!! //不申请额外空间,即不申请 record数组记录信息 可以采用出现即标记为负数的办法~~hhhh真厉害

88. Merge Sorted Array [easy] (Python)

题目链接 https://leetcode.com/problems/merge-sorted-array/ 题目原文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold

Leetcode 26. Remove Duplicates from Sorted Array (easy)

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Exam