Skip to content

Commit b94a0e6

Browse files
ADD: 162-Leetcode(Find Peak element) (#166)
* ADD: 162-Leetcode(Find Peak element) * fix: Graph Portion in Readme.md file
1 parent d3e4d25 commit b94a0e6

File tree

2 files changed

+141
-109
lines changed

2 files changed

+141
-109
lines changed

JavaScript/findPeakElement.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given an integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
3+
4+
Example 1:
5+
Input: nums = [1,2,3,1]
6+
Output: 2
7+
Explanation: 3 is a peak element and your function should return the index number 2.
8+
9+
Example 2:
10+
Input: nums = [1,2,1,3,5,6,4]
11+
Output: 5
12+
Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
13+
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var findPeakElement = function (nums) {
21+
let left = 0,
22+
right = nums.length - 1,
23+
mid;
24+
25+
while (left < right) {
26+
mid = Math.floor((right + left) / 2);
27+
if (nums[mid] > nums[mid + 1]) right = mid;
28+
else left = mid + 1;
29+
}
30+
return left;
31+
};

0 commit comments

Comments
 (0)