惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
J
Java Code Geeks
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
腾讯CDC
博客园 - 司徒正美
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
I
InfoQ
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
美团技术团队
The Cloudflare Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Martin Fowler
Martin Fowler
V
Visual Studio Blog

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree When Dijkstra Algorithm Should be Use? 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
845. Longest Mountain in Array
北叶青藤 · 2026-01-02 · via 博客园 - 北叶青藤

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.

Example 1:

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.

Example 2:

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

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 104

Follow up:

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

Using extra space and 3 passes

Using 2 arrays to record the longest increasing and decrasing from current number. Then join both values to find the longest mountain. 

 1 class Solution:
 2     def longestMountain(self, arr: List[int]) -> int:
 3         if not arr or len(arr) < 3:
 4             return 0
 5         array_len = len(arr)
 6         increasing, decreasing = [0] * array_len, [0] * array_len
 7         for i in range (1, array_len):
 8             if arr[i] > arr[i - 1]:
 9                 increasing[i] = increasing[i - 1] + 1
10         for i in range (array_len - 2, -1, -1):
11             if arr[i] > arr[i + 1]:
12                 decreasing[i] = decreasing[i + 1] + 1
13         
14         max_length = 0
15         for i in range(1, len(arr) - 1):
16             if increasing[i] and decreasing[i]:
17                 max_length = max(max_length, increasing[i] + decreasing[i] + 1)
18         return max_length if max_length >= 3 else 0

 Approach 2: using one pass and no extra arrays. The idea is if there is a mountain, we must have up hill and down hill at the same time. 

 1 class Solution:
 2     def longestMountain(self, arr: List[int]) -> int:
 3         if not arr or len(arr) < 3:
 4             return 0
 5         i = 1
 6         max_len = 0
 7         arr_len = len(arr)
 8         while i < arr_len:
 9             while i < arr_len and arr[i] == arr[i - 1]:
10                 i += 1
11             increasing_count = decreasing_count = 0
12             while i < arr_len and arr[i - 1] < arr[i]:
13                 increasing_count += 1
14                 i += 1
15             
16             while i < arr_len and arr[i - 1] > arr[i]:
17                 decreasing_count += 1
18                 i += 1
19             
20             if increasing_count and decreasing_count:
21                 max_len = max(max_len, increasing_count + decreasing_count + 1)
22             
23         return max_len