You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.
Return an array that consists of indices of peaks in the given array in any order.
Notes:
Example 1:
Input: mountain = [2,4,4] Output: [] Explanation: mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array. mountain[1] also can not be a peak because it is not strictly greater than mountain[2]. So the answer is [].
Example 2:
Input: mountain = [1,4,3,8,5] Output: [1,3] Explanation: mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array. mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1]. But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3].
Constraints:
3 <= mountain.length <= 1001 <= mountain[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find all the peaks in a sequence of numbers, the simplest method is to check every number one by one. For each number, we just need to see if it's taller than its immediate neighbors on both sides.
Here's how the algorithm would work step-by-step:
def find_peaks(mountain):
peak_indices = []
# To be a peak, an element must have neighbors, so we skip the first and last elements.
for current_index in range(1, len(mountain) - 1):
# The core condition for an element to be a peak is being strictly larger than both its neighbors.
if mountain[current_index] > mountain[current_index - 1] and mountain[current_index] > mountain[current_index + 1]:
# If the current element satisfies the peak condition, we store its position.
peak_indices.append(current_index)
return peak_indicesThe most efficient way to find the peaks is to perform a single, straightforward scan through the list of numbers. Since a peak is defined only by its immediate neighbors, we can check each number just once to see if it qualifies.
Here's how the algorithm would work step-by-step:
def find_peaks(mountain_heights):
peak_indices = []
# We only check interior numbers, as the first and last elements lack two neighbors.
for current_index in range(1, len(mountain_heights) - 1):
is_taller_than_left = mountain_heights[current_index] > mountain_heights[current_index - 1]
is_taller_than_right = mountain_heights[current_index] > mountain_heights[current_index + 1]
# A number is a peak if it is strictly greater than both of its immediate neighbors.
if is_taller_than_left and is_taller_than_right:
# If the conditions for a peak are met, we record its location (index).
peak_indices.append(current_index)
return peak_indices| Case | How to Handle |
|---|---|
| Input array has the minimum allowed length of 3 | The solution correctly checks only the middle element at index 1 as a potential peak. |
| Input array has no peaks, such as a monotonically increasing or decreasing array | The loop will complete without finding any elements that satisfy the peak condition, correctly returning an empty list. |
| Input array contains all identical values | The strict inequality check ensures no element is considered a peak since it can never be strictly greater than its neighbors. |
| Input array contains a 'plateau' of identical values | The 'strictly greater' requirement correctly prevents any element on the plateau from being identified as a peak. |
| The overall maximum value in the array is at the first or last position | The solution correctly ignores endpoints as potential peaks by iterating only from the second to the second-to-last element. |
| The input array contains multiple valid peaks | The solution should correctly identify and collect the indices for all elements that meet the peak definition. |
| An element has one equal neighbor and one smaller neighbor | The condition that a peak must be strictly greater than both neighbors correctly disqualifies such an element. |
| Inputs with values at the specified boundaries, such as 1 and 100 | The solution's logic is based on relative comparisons, so the absolute magnitude of the numbers does not affect its correctness. |