EasyNo. 0896
Monotonic Array — LeetCode 896 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
An array is monotonic if it is either monotone increasing or monotone decreasing. An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j].
Example
- Input
- nums = [1,2,2,3]
- Output
- true
Python solution
class Solution:
def isMonotonic(self, nums: List[int]) -> bool:
asc = all(a <= b for a, b in pairwise(nums))
desc = all(a >= b for a, b in pairwise(nums))
return asc or descComplexity
- Time
- O(n)where n is the length of the array
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 896. Monotonic Array?
LeetCode 896. Monotonic Array is rated Easy on LeetCode.
- What is the time complexity of LeetCode 896. Monotonic Array?
The Python solution on this page runs in O(n), where n is the length of the array.
- What is the space complexity of LeetCode 896. Monotonic Array?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 896. Monotonic Array cover?
LeetCode 896. Monotonic Array is tagged Array on LeetCode.