162.Find Peak Element
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[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 num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
O(n) sequential scan
1 | public class Solution { |
O(logn) iterative binary search solution
Lets say you have a mid number at index x, nums[x]. If (num[x+1] > nums[x]), that means a peak element HAS to exist on the right half of that array, because (since every number is unique) 1. the numbers keep increasing on the right side, and the peak will be the last element. 2. the numbers stop increasing and there is a ‘dip’, or there exists somewhere a number such that nums[y] < nums[y-1], which means number[x] is a peak element.
This same logic can be applied to the left hand side (nums[x] < nums[x-1]).
1 | public class Solution { |
O(logn) recursive binary search solution
1 | public class Solution { |