I recognized the peak-finding pattern pretty fast, which was both helpful and a little dangerous.
Use binary search to find a local minimum by comparing the middle element with its neighbors. If the middle is greater than its left neighbor, a local minimum must exist on the left; otherwise, it exists on the right. This works because the array boundaries are treated as positive infinity, guaranteeing at least one local minimum.
Pro tip: Explicitly state that the algorithm relies on the fact that a local minimum is guaranteed to exist due to the infinite boundaries, and that the binary search effectively follows the 'downhill' direction. This shows deep understanding of the invariant and avoids off-by-one errors.
Confirm that the array is non-empty, elements are integers, and out-of-bounds neighbors are positive infinity. Restate that we need any local minimum index in O(log n) time.
Maintain that a local minimum exists within the current search range [low, high]. Initially, the whole array is the range, and the invariant holds because the boundaries are positive infinity.
Compute mid = (low + high) // 2. Compare nums[mid] with nums[mid-1] (or +inf if mid=0) and nums[mid+1] (or +inf if mid=n-1). If nums[mid] is smaller than both, return mid.
If nums[mid] > nums[mid-1], then a local minimum must exist in the left half, so set high = mid - 1. Otherwise, nums[mid] > nums[mid+1], so set low = mid + 1. This maintains the invariant.
When low > high, the search ends, but the invariant guarantees we would have found a local minimum before that. The algorithm runs in O(log n) time and O(1) space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.