Monotonic Array — LeetCode 896 Python Solution
EasyArray
- Problem
- #896
- 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array |
| Space | O(1) auxiliary |
Related problems
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.