File: valid-mountain-array/solution.py

Date: 2026-06-06

Time: 19:38

Purpose

This file solves LeetCode 941 — Valid Mountain Array. It determines whether an integer array forms a "mountain": strictly increasing to a single peak, then strictly decreasing. The file owns the core algorithm; its test file (valid-mountain-array/test_solution.py) validates it.

Key Components

Solution.validMountainArray(arr: List[int]) -> bool — The only method. Given a list of integers, returns True if and only if the array is a valid mountain.

A valid mountain requires:

Patterns

Two-pointer walk-inward. Instead of a single linear scan with state tracking, the solution walks pointer i up from the left and pointer j down from the right, each following its respective slope. If the array is a valid mountain, both pointers converge on the same peak index. This is a clean alternative to the more common single-pass-with-state approach — it avoids explicit state flags for "ascending" vs "descending" phases.

Dependencies

Flow

1. Short-circuit: If len(arr) < 3, return False immediately — a mountain needs at least three elements.

2. Left walk: Starting at i = 0, advance i rightward while arr[i] < arr[i+1]. When the loop exits, i sits at the first position where the array stops increasing — the candidate peak from the left.

3. Right walk: Starting at j = n-1, advance j leftward while arr[j] < arr[j-1]. When the loop exits, j sits at the first position where the array stops decreasing (reading right-to-left) — the candidate peak from the right.

4. Convergence check: Return True only if i == j (both found the same peak) and i != 0 and j != n-1 (the peak isn't at either endpoint, which would mean there's no ascending or descending portion).

Invariants

Error Handling

None — the method assumes valid input per LeetCode's contract (a list of integers). No exceptions are raised or caught. Invalid inputs like None or non-list types would produce an unhandled TypeError at the len() call.

Topics to Explore

Beliefs