Skip to content

2951. Find the Peaks 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  vector<int> findPeaks(vector<int>& mountain) {
    vector<int> ans;
    for (int i = 1; i + 1 < mountain.size(); ++i)
      if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1])
        ans.push_back(i);
    return ans;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public List<Integer> findPeaks(int[] mountain) {
    List<Integer> ans = new ArrayList<>();
    for (int i = 1; i + 1 < mountain.length; ++i)
      if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1])
        ans.add(i);
    return ans;
  }
}
1
2
3
4
class Solution:
  def findPeaks(self, mountain: list[int]) -> list[int]:
    return [i for i in range(1, len(mountain) - 1)
            if mountain[i - 1] < mountain[i] > mountain[i + 1]]