EasyNo. 0941

Valid Mountain Array — LeetCode 941 Python Solution

Topics
Array
Reading time
2 min

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

PythonSolution
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 == j

Complexity

Time
O(n)where n is the length of the array
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.