Loading problem...
A local maximum (also known as a peak) is an element that is strictly greater than both of its immediate neighbors.
Given a 0-indexed integer array heights, find any local maximum element and return its index. If the array contains multiple local maxima, returning the index of any one of them is acceptable.
For boundary conditions, imagine that elements outside the array have a value of negative infinity (-∞). This means:
Important Constraint: Your solution must achieve O(log n) time complexity. A simple linear scan is not acceptable for this problem.
heights = [1,2,3,1]2The value at index 2 is 3, which is greater than its neighbors: heights[1] = 2 and heights[3] = 1. Therefore, index 2 is a valid local maximum.
heights = [1,2,1,3,5,6,4]5The value 6 at index 5 is greater than its neighbors (heights[4] = 5 and heights[6] = 4). Index 1 (value 2) is also a valid answer since 2 > 1 on both sides. Either index 1 or index 5 would be accepted.
heights = [5]0With a single element, the only index (0) is automatically a local maximum since there are no neighbors, and conceptually both neighbors are -∞.
Constraints