↓Skip to main content
  1. LeetCode/

LeetCode 941: Valid Mountain Array

·2 mins· ·
LeetCode Easy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-26

  • Submissions: Runtime Error → Wrong Answer ×2 → Accepted ×4

Approach
#

Walk the monotonic runs (Array Template 1): from 0, advance while strictly rising; where it stops is the peak, and the peak cannot be the first or last element (no ascent or no descent). Then advance while strictly falling; you must land exactly on the last element. Equal neighbors stop both walks and fail naturally.

My Attempts
#

  • Attempt 1 looped over range(len(arr)) while reading arr[i + 1], overrunning the end.
  • Attempt 2 only forbade rising after falling, without requiring both an ascent and a descent: [3] returned True.
  • Attempt 3 returned increase, so the purely rising [0, 2] still returned True. Requiring a rise before any fall and returning increase and decrease passed.

Solution
#

class Solution:
    def validMountainArray(self, arr: List[int]) -> bool:
        n, i = len(arr), 0
        while i + 1 < n and arr[i] < arr[i + 1]:
            i += 1
        if i == 0 or i == n - 1:
            return False
        while i + 1 < n and arr[i] > arr[i + 1]:
            i += 1
        return i == n - 1

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Both an ascent and a descent must exist; purely rising or falling arrays fail.
  • Arrays shorter than 3 never qualify.

Interview Explanation
#

I climb from the left to the peak, which must not be at either end, then descend and require reaching the last element exactly. Time O(n), space O(1).

Related

LeetCode 929: Unique Email Addresses
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 929 note: split at @, normalize only the local part (drop everything after +, remove dots), rejoin with the domain and collect in a set
LeetCode 944: Delete Columns to Make Sorted
·1 min
LeetCode Easy Array String Longest-Increasing-Subsequence
LeetCode 944 note: take each column with zip(*strs) and count the columns that are not non-decreasing
LeetCode 953: Verifying an Alien Dictionary
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 953 note: map letters to their alien index and compare each adjacent pair at the first differing character; with no difference, the shorter must come first
LeetCode 977: Squares of a Sorted Array
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 977 note: the largest squares are at the two ends, so compare the ends with two pointers and fill the result from the back
LeetCode 989: Add to Array-Form of Integer
·2 mins
LeetCode Easy Array Math
LeetCode 989 note: treat k as the carry, add from the last element backwards, and keep prepending digits while k remains
LeetCode 905: Sort Array By Parity
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 905 note: a write pointer swaps even numbers to the front, just like move zeroes