Given an array of integers, find any one local minimum in the array. A local minimum is defined as an integer in the array that is less than or equal to its neighbors.
Example:
[5, 9, 11, 14, 7, 8] -> return 5 or 7
这道题要求在整数数组中找到任意一个局部最小值,即某个元素不大于相邻元素。典型做法是利用数组的相对变化进行二分查找:比较中点与左右邻居的大小关系,根据“下降 / 上升”的趋势缩小搜索范围,从而把时间复杂度做到 O(log n)。如果数组边界元素满足条件,也可以直接返回。
正文完