[LeetCode] Find the Peaks

2951. Find the Peaks

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:

  • A peak is defined as an element that is strictly greater than its neighboring elements.
  • The first and last elements of the array are not a peak.
1
2
3
4
5
6
7
8
9
10
class Solution {
public:
vector<int> findPeaks(vector<int>& A) {
vector<int> res;
for(int i = 1; i < A.size() - 1; i ++) {
if(A[i] > A[i-1] and A[i] > A[i+1]) res.push_back(i);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/03/PS/LeetCode/find-the-peaks/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.