返回介绍

solution / 2900-2999 / 2951.Find the Peaks / README_EN

发布于 2024-06-17 01:02:58 字数 4148 浏览 0 评论 0 收藏 0

2951. Find the Peaks

中文文档

Description

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.

 

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 <= 100
  • 1 <= mountain[i] <= 100

Solutions

Solution 1: Direct Traversal

We directly traverse the index $i \in [1, n-2]$. For each index $i$, if $mountain[i-1] < mountain[i]$ and $mountain[i + 1] < mountain[i]$, then $mountain[i]$ is a peak, and we add index $i$ to the answer array.

After the traversal ends, we return the answer array.

The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.

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]
    ]
class Solution {
  public List<Integer> findPeaks(int[] mountain) {
    List<Integer> ans = new ArrayList<>();
    for (int i = 1; i < mountain.length - 1; ++i) {
      if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
        ans.add(i);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findPeaks(vector<int>& mountain) {
    vector<int> ans;
    for (int i = 1; i < mountain.size() - 1; ++i) {
      if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
        ans.push_back(i);
      }
    }
    return ans;
  }
};
func findPeaks(mountain []int) (ans []int) {
  for i := 1; i < len(mountain)-1; i++ {
    if mountain[i-1] < mountain[i] && mountain[i+1] < mountain[i] {
      ans = append(ans, i)
    }
  }
  return
}
function findPeaks(mountain: number[]): number[] {
  const ans: number[] = [];
  for (let i = 1; i < mountain.length - 1; ++i) {
    if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
      ans.push(i);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文