Find the Peaks — LeetCode 2951 Python Solution
EasyArrayEnumeration
- Problem
- #2951
- 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array |
| Space | O(1) auxiliary |
Related problems
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.