其他分享
首页 > 其他分享> > Monotonic Array LT896

Monotonic Array LT896

作者:互联网

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)

 1 class Solution {
 2     private boolean isMonotonicIncreasing(int[] A) {
 3         int i = 1;
 4         while(i < A.length && A[i-1] <= A[i]) {
 5             ++i;
 6         }
 7         return i == A.length;
 8     }
 9     
10     private boolean isMonotonicDecreasing(int[] A) {
11         int i = 1;
12         while(i < A.length && A[i-1] >= A[i]) {
13             ++i;
14         }
15         return i == A.length;
16     }
17     
18     public boolean isMonotonic(int[] A) { 
19         return isMonotonicIncreasing(A) || isMonotonicDecreasing(A);
20     }
21 }

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

 1 class Solution {
 2     private boolean isMonotonicIncreasing(int[] A) {
 3         for(int i = 1; i < A.length; ++i) {
 4             if(A[i-1] > A[i]) {
 5                 return false;
 6             }
 7         }
 8         return true;
 9     }
10     
11     private boolean isMonotonicDecreasing(int[] A) {
12         for(int i = 1; i < A.length; ++i) {
13             if(A[i-1] < A[i]) {
14                 return false;
15             }
16         }
17         return true;
18     }
19     
20     public boolean isMonotonic(int[] A) { 
21         return isMonotonicIncreasing(A) || isMonotonicDecreasing(A);
22     }
23 }

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)

 1 class Solution {
 2  
 3     public boolean isMonotonic(int[] A) { 
 4         int flag = 0;
 5         for(int i = 1; i < A.length; ++i) {
 6             int c = Integer.compare(A[i-1], A[i]);
 7             if(c == 0) {
 8                 continue;
 9             }
10             
11             if(flag != 0 && c != flag) {
12                 return false;
13             }
14             flag = c;
15         }
16         
17         return true;
18     }
19 }

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

 1 class Solution {
 2     public boolean isMonotonic(int[] A) { 
 3        boolean increasing = false;
 4        boolean decreasing = false;
 5        for(int i = 1; i < A.length; ++i) {
 6            if(A[i-1] > A[i]) {
 7                increasing = true;
 8            }
 9            else if(A[i-1] < A[i]) {
10                decreasing = true;
11            }
12        }
13         
14        if(increasing && decreasing) {
15            return false;
16        }
17         
18        return true;
19     }
20 }

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

 1 class Solution {
 2     public boolean isMonotonic(int[] A) { 
 3        boolean increasing = true;
 4        boolean decreasing = true;
 5        for(int i = 1; i < A.length; ++i) {
 6            if(A[i-1] < A[i]) {
 7                increasing = false;
 8            }
 9            else if(A[i-1] > A[i]) {
10                decreasing = false;
11            }
12        }
13         
14        return increasing || decreasing;
15     }
16 }

标签:return,int,Monotonic,LT896,boolean,decreasing,Array,increasing,true
来源: https://www.cnblogs.com/taste-it-own-it-love-it/p/10703036.html