Valid Mountain Array (LeetCode 941): Solution and Explanation
You get an integer array. Return true if it strictly rises to a single peak and then strictly falls, with the peak not at either end and at least 3 elements in total.
Intuition
Picture the array as the profile of a hill. A valid mountain has one uphill slope from the left, one downhill slope to the right, and no flat ledges.
Send one climber up from the left end, stepping right only while the next value is strictly higher. Send another up from the right end, stepping left only while the previous value is strictly higher. On a real mountain, both climbers end up standing on the same summit.
Everything that breaks the shape shows up in where they stop. A flat step or a second peak leaves them on different indices. An array that only rises or only falls leaves one climber at its starting end, which means one slope is missing.
Approach: Two climbers from both ends
- Set
i = 0andj = n - 1. - While
i + 1 < nandarr[i] < arr[i + 1], moveiright. - While
j > 0andarr[j - 1] > arr[j], movejleft. - Return true only if
i > 0(there is an uphill),j < n - 1(there is a downhill) andi == j(they meet on one peak).
Step-by-step example
arr = [2,1]
- The left climber starts at i = 0. arr[0] = 2 is not less than arr[1] = 1, so it cannot move and stays at 0.
- The right climber starts at j = 1. arr[0] = 2 > arr[1] = 1, so it steps up to j = 0.
- The climbers meet at index 0, but i = 0 means there is no uphill side, and the peak sits at the left end.
- Return false. With only 2 elements, no inner peak can exist anyway.
Code: Python, Java, C++
1class Solution:
2 def validMountainArray(self, arr: List[int]) -> bool:
3 n = len(arr)
4 i, j = 0, n - 1
5 while i + 1 < n and arr[i] < arr[i + 1]:
6 i += 1
7 while j > 0 and arr[j - 1] > arr[j]:
8 j -= 1
9 return 0 < i == j < n - 11class Solution {
2 public boolean validMountainArray(int[] arr) {
3 int n = arr.length;
4 int i = 0, j = n - 1;
5 while (i + 1 < n && arr[i] < arr[i + 1]) {
6 i++;
7 }
8 while (j > 0 && arr[j - 1] > arr[j]) {
9 j--;
10 }
11 return i > 0 && i == j && j < n - 1;
12 }
13}1class Solution {
2public:
3 bool validMountainArray(vector<int>& arr) {
4 int n = arr.size();
5 int i = 0, j = n - 1;
6 while (i + 1 < n && arr[i] < arr[i + 1]) {
7 i++;
8 }
9 while (j > 0 && arr[j - 1] > arr[j]) {
10 j--;
11 }
12 return i > 0 && i == j && j < n - 1;
13 }
14};Time and space complexity
O(n)
Each climber only moves toward the middle, and together they pass over at most every element once or twice.
O(1)
Only the two indices are stored.
Other approaches
Single pass walk up then down (time O(n), space O(1))
Walk right while the values rise, check that you moved and are not at the end, then keep walking while the values fall, and check that you reached the last index. It is the same idea with one pointer.
Find the maximum, then check both sides (time O(n), space O(1))
Locate the index of the largest value, reject it if it is the first or last index, and verify that the left part strictly increases and the right part strictly decreases. It needs two scans but is easy to reason about.
Common mistakes
- Using
<=instead of<while climbing. Equal neighbours form a flat ledge, and a mountain must be strict on both sides. - Accepting a strictly increasing or strictly decreasing array. The peak must be inside the array, so both climbers must move.
- Forgetting the length check. Arrays with 1 or 2 elements can never be mountains; the climber checks reject them automatically.
- Reading
arr[i + 1]without first checkingi + 1 < n, which goes out of bounds on an increasing array.
Frequently asked questions
Why does checking i == j work?
The left climber stops at the first index where the climb is no longer strictly up, and the right climber stops at the first index from the right where the descent is no longer strictly down. Those are the same index only when the whole array is one rise followed by one fall.
Is [0, 1, 2] a mountain?
No. It only rises, so the right climber never moves and stays at the last index. The peak would be at the end, which the definition forbids.
How are flat sections handled?
A flat step stops whichever climber reaches it, because neither strict comparison holds. The two climbers then stop on different sides of the flat part, so i != j and the answer is false.