EasyNo. 0941
Valid Mountain Array — LeetCode 941 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given an array of integers arr, return true if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if: arr.length >= 3 There exists some i with 0 < i < arr.length - 1 such that: arr[0] < arr[1] < ...
Example
- Input
- arr = [2,1]
- Output
- false
Python solution
class Solution:
def validMountainArray(self, arr: List[int]) -> bool:
n = len(arr)
if n < 3:
return False
i, j = 0, n - 1
while i + 1 < n - 1 and arr[i] < arr[i + 1]:
i += 1
while j - 1 > 0 and arr[j - 1] > arr[j]:
j -= 1
return i == jComplexity
- 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 941. Valid Mountain Array?
LeetCode 941. Valid Mountain Array is rated Easy on LeetCode.
- What is the time complexity of LeetCode 941. Valid Mountain 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 941. Valid Mountain Array?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 941. Valid Mountain Array cover?
LeetCode 941. Valid Mountain Array is tagged Array on LeetCode.