Solution 1: accepted
Binary search.
Since nums[-1] and num[n] are negative infinite, there must be a peak in the middle. Consider four conditions that mid may have, we can narrow the scope by half each time.
- mid - 1 < mid > mid + 1: mid is the peak.
- mid - 1 < mid < mid + 1: mid < mid + 1 [peak] len - 1 > -infinite, there must be a peak on right side.
- mid - 1 > mid > mid + 1: -infinite < 1 [peak] mid - 1 > mid, there must be a peak on left side.
- mid - 1 > mid < mid + 1: mid is a pit, either side has a peak.
|
|