> ## 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.

# Longest Mountain in Array Python Solution

> Tested Python solution for LeetCode 845 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 845, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Dynamic Programming](/catalog/topics/dynamic-programming), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/longest-mountain-in-array/description/).

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

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

## Problem

You may recall that an array `arr` is a **mountain array** if and only if:

* `arr.length >= 3`
* There exists some index `i` (**0-indexed**) 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]`

Given an integer array `arr`, return *the length of the longest subarray, which is a mountain*. Return `0` if there is no mountain subarray.

### Examples

```
Input: arr = [2,1,4,7,3,2,5]
Output: 5
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.
```

```
Input: arr = [2,2,2]
Output: 0
Explanation: There is no mountain.
```

### Constraints

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

**Follow up:**

* Can you solve it using only one pass?
* Can you solve it in `O(1)` space?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def longest_mountain(self, arr: list[int]) -> int:
        n = len(arr)
        longest = 0
        i = 1
        while i < n:
            if arr[i - 1] < arr[i]:
                start = i - 1
                while i < n and arr[i - 1] < arr[i]:
                    i += 1
                peak = i - 1
                while i < n and arr[i - 1] > arr[i]:
                    i += 1
                if peak < i - 1:
                    longest = max(longest, i - start)
            else:
                i += 1
        return longest
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


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