> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Valid Mountain Array Python Solution

> Tested Python solution for LeetCode 941 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 941, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array). [View on LeetCode](https://leetcode.com/problems/valid-mountain-array/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 941   # by problem number
lcpy gen -s valid_mountain_array   # by problem name
```

## 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] < ... < arr[i - 1] < arr[i]`
  * `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`

![Example](https://assets.leetcode.com/uploads/2019/10/20/hint_valid_mountain_array.png)

### Examples

```
Input: arr = [2,1]
Output: false
```

```
Input: arr = [3,5,5]
Output: false
```

```
Input: arr = [0,3,2,1]
Output: true
```

### Constraints

* 1 \<= arr.length \<= 10^4
* 0 \<= arr\[i] \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_mountain_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_mountain_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def valid_mountain_array(self, arr: list[int]) -> bool:
        i = 0
        n = len(arr)
        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 | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.