Loading section...
Find Peak Element
Concepts: pyPeakElement, pyDirectionalBinarySearch
LeetCode 162. Find Peak Element. A peak is any element strictly greater than its neighbors. The array may not be sorted. Binary search should not work on an unsorted array -- or so the candidate thinks. The insight that makes this problem click is that binary search does not require global sortedness. It only requires that you can determine which half of the current search space contains an answer. And for peak finding, you can. The Key Insight Compare nums[mid] with nums[mid+1]. If nums[mid] < nums[mid+1], the right side is locally ascending. A peak must exist in the right half (either nums[mid+1] itself is a peak, or there is a higher value further right that creates a peak). If nums[mid] > nums[mid+1], the left half contains a peak by the same logic. You eliminate half the search space