Longest Mountain in Array (LeetCode 845): Solution and Explanation
Return the length of the longest subarray that strictly rises and then strictly falls, with at least three elements.
Intuition
A mountain is two runs joined at a peak: a strictly increasing run and then a strictly decreasing run. Both runs need at least one step.
If we stand at a left foot, we can walk up as long as values rise, then walk down as long as values fall. The place where we stop is the right foot, and that whole stretch is the biggest mountain starting at this foot.
Two mountains can share at most one index (the right foot of one is the left foot of the next), so after finishing a stretch we jump straight to its end. Every index is passed a constant number of times.
Approach: Two pointers over slopes
- Set best = 0 and base = 0.
- Let end = base. If arr[end] < arr[end + 1], move end up while values strictly rise.
- If the next value is strictly lower, move end down while values strictly fall, then update best with end - base + 1.
- A flat step or the end of the array right after the climb means no mountain here.
- Move base to max(end, base + 1) and repeat until base reaches n.
Step-by-step example
arr = [2,1,4,7,3,2,5]
- base = 0: 2 is not below 1, so no climb starts. base moves to 1.
- base = 1: 1 < 4, so end climbs through 4 and 7 to index 3, the peak.
- 3 is lower than 7, so end descends to index 4 (3) and index 5 (2). 5 is higher than 2, so the descent stops.
- The mountain is indices 1..5, length 5. best = 5.
- base jumps to 5. 2 < 5 starts a climb, but the array ends at the top, so there is no descent.
- Return 5.
Code: Python, Java, C++
1class Solution:
2 def longestMountain(self, arr: List[int]) -> int:
3 n = len(arr)
4 best = 0
5 base = 0
6 while base < n:
7 end = base
8 if end + 1 < n and arr[end] < arr[end + 1]:
9 while end + 1 < n and arr[end] < arr[end + 1]:
10 end += 1
11 if end + 1 < n and arr[end] > arr[end + 1]:
12 while end + 1 < n and arr[end] > arr[end + 1]:
13 end += 1
14 best = max(best, end - base + 1)
15 base = max(end, base + 1)
16 return best1class Solution {
2 public int longestMountain(int[] arr) {
3 int n = arr.length;
4 int best = 0;
5 int base = 0;
6 while (base < n) {
7 int end = base;
8 if (end + 1 < n && arr[end] < arr[end + 1]) {
9 while (end + 1 < n && arr[end] < arr[end + 1]) {
10 end++;
11 }
12 if (end + 1 < n && arr[end] > arr[end + 1]) {
13 while (end + 1 < n && arr[end] > arr[end + 1]) {
14 end++;
15 }
16 best = Math.max(best, end - base + 1);
17 }
18 }
19 base = Math.max(end, base + 1);
20 }
21 return best;
22 }
23}1class Solution {
2public:
3 int longestMountain(vector<int>& arr) {
4 int n = arr.size();
5 int best = 0;
6 int base = 0;
7 while (base < n) {
8 int end = base;
9 if (end + 1 < n && arr[end] < arr[end + 1]) {
10 while (end + 1 < n && arr[end] < arr[end + 1]) {
11 end++;
12 }
13 if (end + 1 < n && arr[end] > arr[end + 1]) {
14 while (end + 1 < n && arr[end] > arr[end + 1]) {
15 end++;
16 }
17 best = max(best, end - base + 1);
18 }
19 }
20 base = max(end, base + 1);
21 }
22 return best;
23 }
24};Time and space complexity
O(n)
base only moves forward and always jumps to end after a slope, so each index is visited a constant number of times.
O(1)
Only a few integer pointers are used.
Other approaches
Up and down arrays (time O(n), space O(n))
Precompute up[i], the length of the rising run ending at i, and down[i], the falling run starting at i. Every i with both positive is a peak, and the mountain length is up[i] + down[i] + 1.
Expand around each peak (time O(n), space O(1))
Find every index that is higher than both neighbors and expand left and right. Simple, and still linear because slopes of different peaks do not overlap much, but easier to get wrong.
Common mistakes
- Treating equal neighbors as part of a slope. Both sides must be strictly monotonic, so a plateau breaks the mountain.
- Counting a pure climb or a pure descent as a mountain. You need at least one step up and one step down.
- Advancing base by only one after a mountain, which makes the scan revisit slopes. Jump to end instead.
- Reading arr[end + 1] without checking end + 1 < n first.
Frequently asked questions
Why can base jump straight to end?
Any mountain that starts strictly inside the stretch base..end would start on a slope that is already rising or falling into this one, so it cannot be longer. The only index a new mountain can share is the right foot, end itself.
What happens with a plateau like [1,3,3,2]?
The climb stops at the first 3 because 3 is not less than 3, and the next value is not lower either. So no mountain is recorded, and the answer is 0.
Can this be done in one pass with O(1) space?
Yes. This two-pointer walk is one pass and keeps only base, end and best.