File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 659
659
849|[ Maximize Distance to Closest Person] ( ./0849-maximize-distance-to-closest-person.js ) |Medium|
660
660
850|[ Rectangle Area II] ( ./0850-rectangle-area-ii.js ) |Hard|
661
661
851|[ Loud and Rich] ( ./0851-loud-and-rich.js ) |Medium|
662
+ 852|[ Peak Index in a Mountain Array] ( ./0852-peak-index-in-a-mountain-array.js ) |Medium|
662
663
867|[ Transpose Matrix] ( ./0867-transpose-matrix.js ) |Easy|
663
664
868|[ Binary Gap] ( ./0868-binary-gap.js ) |Easy|
664
665
872|[ Leaf-Similar Trees] ( ./0872-leaf-similar-trees.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 852. Peak Index in a Mountain Array
3
+ * https://leetcode.com/problems/peak-index-in-a-mountain-array/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given an integer mountain array arr of length n where the values increase to a peak
7
+ * element and then decrease.
8
+ *
9
+ * Return the index of the peak element.
10
+ *
11
+ * Your task is to solve it in O(log(n)) time complexity.
12
+ */
13
+
14
+ /**
15
+ * @param {number[] } arr
16
+ * @return {number }
17
+ */
18
+ var peakIndexInMountainArray = function ( arr ) {
19
+ let left = 0 ;
20
+ let right = arr . length - 1 ;
21
+
22
+ while ( left < right ) {
23
+ const middle = Math . floor ( ( left + right ) / 2 ) ;
24
+
25
+ if ( arr [ middle ] > arr [ middle + 1 ] ) {
26
+ right = middle ;
27
+ } else {
28
+ left = middle + 1 ;
29
+ }
30
+ }
31
+
32
+ return left ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments