> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/801-1000/845.-shu-zu-zhong-de-zui-chang-shan-mai.md).

# 845. 数组中的最长山脉

<https://leetcode-cn.com/problems/longest-mountain-in-array/>

## 解法一：

一次遍历，找山顶（比两端都高）。若找到，从山顶向两端发散，求山脉长度，每次更新最大长度。

```python
class Solution:
    def longestMountain(self, A: List[int]) -> int:
        n = len(A)
        maxL = 0
        for i in range(n):
            length = 0  #山脉长度
            #找到山顶
            if i-1 >= 0 and i+1 < n and A[i] > A[i-1] and A[i] > A[i+1]:
                length += 1
                left = right = i  #山顶左右两侧指针
                while left-1 >= 0 and A[left] > A[left-1]:
                    left -= 1
                    length += 1
                while right+1 < n and A[right] > A[right+1]:
                    right += 1
                    length += 1
            maxL = max(length, maxL)
        return maxL
```
