Created
November 9, 2021 21:14
-
-
Save les-peters/d65292d7d5d3a7112b6df08e5533af81 to your computer and use it in GitHub Desktop.
Local Peaks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| question = """ | |
| Given an array of integers, return the index of each local peak in the array. | |
| A “peak” element is an element that is greater than its neighbors. | |
| $ localPeaks([1,2,3,1]) | |
| $ [2] | |
| $ localPeaks([1,3,2,3,5,6,4]) | |
| $ [1, 5] | |
| """ | |
| def localPeaks(a): | |
| peaks = [] | |
| for i in range(1, len(a) - 1): | |
| if a[i] > a[i-1] and a[i] > a[i+1]: | |
| peaks.append(i) | |
| return peaks | |
| print(localPeaks([1,2,3,1])) | |
| print(localPeaks([1,3,2,3,5,6,4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment