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

推荐订阅源

有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
V
V2EX
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
月光博客
月光博客
云风的 BLOG
云风的 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