Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c09c278

Browse files
2951. Find the Peaks
1 parent 4ece285 commit c09c278

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎2501-3000/2951. Find the Peaks.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
3+
// Solution by Sergey Leschev
4+
// 2951. Find the Peaks
5+
6+
// Time complexity: O(n)
7+
// Space complexity: O(1)
8+
9+
func findPeaks(_ mountain: [Int]) -> [Int] {
10+
let n = mountain.count
11+
var peaks = [Int]()
12+
13+
for i in 1..<(n - 1) {
14+
if mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1] {
15+
peaks.append(i)
16+
}
17+
}
18+
19+
return peaks
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /