bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 896. Monotonic Array

896. Monotonic Array

Leetcode Solutions

Approach: Two Pass

  1. Initialize two boolean variables, increasing and decreasing, to true.
  2. Iterate over the array from the first element to the second-to-last element.
    • If nums[i] > nums[i+1], set increasing to false.
    • If nums[i] < nums[i+1], set decreasing to false.
  3. After the loop, if either increasing or decreasing is true, return true.
  4. If neither is true, return false.
UML Thumbnail

Approach: One Pass (Simple Variant)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...