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

推荐订阅源

Y
Y Combinator Blog
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
The Cloudflare Blog
I
Intezer
P
Privacy International News Feed
C
Check Point Blog
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
B
Blog
Latest news
Latest news
小众软件
小众软件
Engineering at Meta
Engineering at Meta
Cyberwarzone
Cyberwarzone
Project Zero
Project Zero
P
Proofpoint News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
美团技术团队
G
GRAHAM CLULEY
博客园 - 司徒正美
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
C
Cisco Blogs
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
T
Threat Research - Cisco Blogs
月光博客
月光博客
Security Latest
Security Latest
Scott Helme
Scott Helme
T
Tenable 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