EasyNo. 2951
Find the Peaks — LeetCode 2951 Python Solution
- Topics
- Array, Enumeration
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.
Example
- 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.
Python solution
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]
]Complexity
- Time
- O(n)where n is the length of the array
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1534 | Count Good Triplets | Easy |
| 1566 | Detect Pattern of Length M Repeated K or More Times | Easy |
| 1620 | Coordinate With Maximum Network Quality | Medium |
| 2735 | Collecting Chocolates | Medium |
| 2765 | Longest Alternating Subarray | Easy |
| 2778 | Sum of Squares of Special Elements | Easy |
Frequently asked questions
- How hard is LeetCode 2951. Find the Peaks?
LeetCode 2951. Find the Peaks is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2951. Find the Peaks?
The Python solution on this page runs in O(n), where n is the length of the array.
- What is the space complexity of LeetCode 2951. Find the Peaks?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2951. Find the Peaks cover?
LeetCode 2951. Find the Peaks is tagged Array and Enumeration on LeetCode.