← Home

896. Monotonic Array

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
896

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 896. Monotonic Array, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isMonotonic.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N), where N is the length of A.
  • Space: O(1).

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02An array is monotonic if it is either monotone increasing or monotone decreasing.
03
04An 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].
05
06Return true if and only if the given array A is monotonic.
07
08 
09
10Example 1:
11
12Input: [1,2,2,3]
13Output: true
14Example 2:
15
16Input: [6,5,4,4]
17Output: true
18Example 3:
19
20Input: [1,3,2]
21Output: false
22Example 4:
23
24Input: [1,2,4,5]
25Output: true
26Example 5:
27
28Input: [1,1,1]
29Output: true
30 
31
32Note:
33
341 <= A.length <= 50000
35-100000 <= A[i] <= 100000
36**/
37
38/**
39Time Complexity: O(N), where N is the length of A.
40Space Complexity: O(1). 
41**/
42
43//Runtime: 88 ms, faster than 52.38% of C++ online submissions for Monotonic Array.
44//Memory Usage: 16.1 MB, less than 9.16% of C++ online submissions for Monotonic Array.
45class Solution {
46public:
47    bool isMonotonic(vector<int>& A) {
48        bool isIncreasing = true, isDecreasing = true;
49        //length 1 or 2 A is default to monotonic
50        for(int i = 0; i < A.size()-1; i++){
51            if(A[i+1]-A[i]<0){
52                isIncreasing = false;
53            }
54            if(A[i+1]-A[i]>0){
55                isDecreasing = false;
56            }
57            if(!isIncreasing && !isDecreasing){
58                return false;
59            }
60        }
61        return isIncreasing || isDecreasing;
62    }
63};

Cost

Complexity

Time
O(N), where N is the length of A.
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(1).
Auxiliary state plus the answer structure where the problem requires one.