8000 Add 2951_find_the_peaks · m3xw3ll/LeetCode@8eb9714 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8eb9714

Browse files
committed
Add 2951_find_the_peaks
1 parent 020a8ea commit 8eb9714

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Algorithms/2951_find_the_peaks.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [Find the Peaks](https://leetcode.com/problems/find-the-peaks/description/)
2+
3+
You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.
4+
5+
Return an array that consists of indices of peaks in the given array in any order.
6+
7+
Notes:
8+
9+
- A peak is defined as an element that is strictly greater than its neighboring elements.
10+
- The first and last elements of the array are not a peak.
11+
12+
Example 1:
13+
```
14+
Input: mountain = [2,4,4]
15+
Output: []
16+
Explanation: mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
17+
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
18+
So the answer is [].
19+
```
20+
Example 2:
21+
```
22+
Input: mountain = [1,4,3,8,5]
23+
Output: [1,3]
24+
Explanation: mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
25+
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
26+
But mountain [1] and mountain[3] are strictly greater than their neighboring elements.
27+
So the answer is [1,3].
28+
```
29+
Solution
30+
```python
31+
class Solution:
32+
def findPeaks(self, mountain: List[int]) -> List[int]:
33+
peaks = []
34+
for i in range(1, len(mountain) - 1):
35+
if mountain[i - 1] < mountain[i] > mountain[i+1]:
36+
peaks.append(i)
37+
return peaks
38+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def find_peaks(mountain):
2+
peaks = []
3+
for i in range(1, len(mountain) - 1):
4+
if mountain[i - 1] < mountain[i] > mountain[i+1]:
5+
peaks.append(i)
6+
return peaks
7+
8+
9+
print(find_peaks([1, 4, 3, 8, 5]))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
746746
| 2937 | [Make Three Strings Equal](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2937_make_three_strings_equal.md) |
747747
| 2942 | [Find Words Containing Character](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2942_find_words_containing_character.md) |
748748
| 2946 | [Matrix Similarity After Cyclic Shifts](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2946_matrix_similiarity_after_cyclic_shifts.md) |
749+
| 2951 | [Find the Peaks](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2951_find_the_peaks.md)
749750
| 2956 | [Find Common Elements Between Two Arrays](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2956_find_common_elements_between_two_arrays.md) |
750751
| 2974 | [Minimum Number Game](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2974_minimum_number_game.md) |
751752
| 2965 | [Find Missing and Repeated Values](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/2965_find_missing_and_repeated_values.md)

0 commit comments

Comments
 (0)
0