File tree 1 file changed +7
-7
lines changed
scripts/algorithms/F/Find Peak Element
1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 63 ms (Top 93.57%) | Memory: 42 MB (Top 69.12%)
1
2
/**
2
3
* @param {number[] } nums
3
4
* @return {number }
4
5
*/
5
6
var findPeakElement = function ( nums ) {
6
7
if ( nums . length === 1 ) return 0 ;
7
-
8
+
8
9
const recursion = ( startIndex , endIndex ) => {
9
- const midIndex = Math . floor ( ( startIndex + endIndex ) / 2 ) ;
10
+ const midIndex = Math . floor ( ( startIndex + endIndex ) / 2 ) ;
10
11
11
12
if ( startIndex === endIndex ) return startIndex ;
12
13
if ( startIndex + 1 === endIndex ) {
13
14
return nums [ endIndex ] >= nums [ startIndex ] ? endIndex : startIndex ;
14
15
}
15
16
16
-
17
17
if ( nums [ midIndex ] > nums [ midIndex - 1 ] && nums [ midIndex ] > nums [ midIndex + 1 ] ) return midIndex ;
18
18
if ( nums [ midIndex ] > nums [ midIndex - 1 ] && nums [ midIndex ] < nums [ midIndex + 1 ] ) return recursion ( midIndex + 1 , endIndex ) ;
19
19
if ( nums [ midIndex ] < nums [ midIndex - 1 ] && nums [ midIndex ] > nums [ midIndex + 1 ] ) return recursion ( startIndex , midIndex - 1 ) ;
20
- if ( nums [ midIndex ] < nums [ midIndex - 1 ] && nums [ midIndex ] < nums [ midIndex + 1 ] )
20
+ if ( nums [ midIndex ] < nums [ midIndex - 1 ] && nums [ midIndex ] < nums [ midIndex + 1 ] )
21
21
return nums [ midIndex - 1 ] > nums [ midIndex + 1 ] ? recursion ( startIndex , midIndex - 1 ) : recursion ( midIndex + 1 , endIndex ) ;
22
-
22
+
23
23
}
24
-
24
+
25
25
return recursion ( 0 , nums . length - 1 ) ;
26
- } ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments