Imagine you're a mountaineer analyzing the elevation profile of a mountain range! You have an array mountain representing the heights at different points along your path. Your mission is to identify all the peaks - those special positions where the elevation is strictly higher than both neighboring points.
What makes a peak? A peak at index i must satisfy: mountain[i-1] < mountain[i] > mountain[i+1]
Important: The first and last elements cannot be peaks (you need neighbors on both sides!)
Goal: Return an array containing the indices of all peaks in any order.
Example: For [1, 4, 3, 8, 5], index 1 (value 4) and index 3 (value 8) are peaks, so return [1, 3].
Input & Output
Visualization
Time & Space Complexity
We visit each element exactly once (except first and last)
Only using a few variables, result array doesn't count toward space complexity
Constraints
- 3 โค mountain.length โค 1000
- 1 โค mountain[i] โค 106
- Array length is at least 3 (needed for peaks to exist)