Find Peak Element Python Leetcode

preview_player
Показать описание
🔴 Binary Search to find Peak Element in Javascript.

🔴 Question Link -

✅Connect with me

🔴 Question with Example
A peak element is an element that is greater than its neighbors.
Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case, return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞.

Example 1:

Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.

Time line
0:00 - Intro
0:32 - Undestand Question
2:04 - Linear Scan Code O(N)
3:54 - Binary Search Explanation
9:42 - Binary Search Code O(logN)
Рекомендации по теме
Комментарии
Автор

Thank you, you explained this perfectly, liked and comment to support!

symbol
Автор

Nice solution. One little suggestion. Remember when doing binary search, your way of finding the middle index can cause an integer overflow if the array is really big since you're adding left and right first. You can use (left + (right - left)/2) to avoid that edge case.

hoodphenomenon
Автор

Find max and return
return nums.index(max(nums))

anig
Автор

If you don't mind me asking, what's the purpose of peak finding ?.

Rantofthings.