Skip to content

Commit ca95df2

Browse files
committed
Runtime: 63 ms (Top 93.57%) | Memory: 42 MB (Top 69.12%)
1 parent e93bda2 commit ca95df2

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1+
// Runtime: 63 ms (Top 93.57%) | Memory: 42 MB (Top 69.12%)
12
/**
23
* @param {number[]} nums
34
* @return {number}
45
*/
56
var findPeakElement = function(nums) {
67
if(nums.length === 1) return 0;
7-
8+
89
const recursion = (startIndex, endIndex) => {
9-
const midIndex = Math.floor((startIndex + endIndex)/2);
10+
const midIndex = Math.floor((startIndex + endIndex)/2);
1011

1112
if (startIndex === endIndex) return startIndex;
1213
if (startIndex + 1 === endIndex) {
1314
return nums[endIndex] >= nums [startIndex] ? endIndex : startIndex;
1415
}
1516

16-
1717
if(nums[midIndex] > nums[midIndex-1] && nums[midIndex] > nums[midIndex+1]) return midIndex;
1818
if(nums[midIndex] > nums[midIndex-1] && nums[midIndex] < nums[midIndex+1]) return recursion(midIndex + 1, endIndex);
1919
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])
2121
return nums[midIndex-1] > nums[midIndex+1] ? recursion(startIndex, midIndex - 1) : recursion(midIndex + 1, endIndex);
22-
22+
2323
}
24-
24+
2525
return recursion(0, nums.length - 1);
26-
};
26+
};

0 commit comments

Comments
 (0)