#2951

Find the Peaks

newbie · 240 · lc easy +18 · verified · 75% accepted · 216 likes · top 86%

Description

Given a 0-indexed array mountain, identify every peak — an interior element that is strictly greater than both its immediate neighbors. The first and last positions can never be peaks.

Return the list of peak indices in any order.

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].

Code

1
2
3