Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4402f1

Browse files
committedMar 20, 2025
Add solution #852
1 parent 181b2cd commit a4402f1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@
659659
849|[Maximize Distance to Closest Person](./0849-maximize-distance-to-closest-person.js)|Medium|
660660
850|[Rectangle Area II](./0850-rectangle-area-ii.js)|Hard|
661661
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|
662663
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
663664
868|[Binary Gap](./0868-binary-gap.js)|Easy|
664665
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.