An array is monotonic if it is either monotone increasing (nums[i] <= nums[j] for all i <= j) or monotone decreasing (nums[i] >= nums[j] for all i <= j). Given an integer array nums, return true if it is monotonic, or false otherwise.
An array is monotonic if it is either monotone increasing (nums[i] <= nums[j] for all i <= j) or monotone decreasing (nums[i] >= nums[j] for all i <= j). Given an integer array nums, return true if it is monotonic, or false otherwise.
Example 1:
Input: {"nums":[1,2,2,3]}
Output: true
Constraints:
1≤nums.length≤105
−105≤nums[i]≤105
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[1,2,2,3]}
Keep two candidate shapes alive: "never goes down" and "never goes up". Each step between neighbours can rule one of them out.
increasing = true
no step down yet
decreasing = true
no step up yet
the array as a path: each step is up, down or flat