Link
题目:
在一个存在峰值的数组中,峰值左边至少存在一个比峰值小的数,峰值右边至少存在一个比峰值大的数。寻找出target value的index值,如果存在多个,返回index最小的那个,如果不存在,返回-1。
看到题之后的第一反应是和 33.搜索螺旋排序数组一样的方法, 但是此题限制了对原始数组的访问次数,所以很多边界判断可能会超出限制,故没有选择33题的解法。
根据题解和评论,反而先找出数组中的峰值,再对两边的升序和降序数组分别二分查找比较方便。
寻找峰值也可以使用二分法,此处给出两个二分法的模版。
通用模版,在此题中可以用于找到峰值:
private int binarySearch(int[] arr, int target){
int lo = 0, hi = arr.length-1;
while (lo < hi){ //注意这里是小于
int mid = lo + (hi - lo)/2; //防止overflow
// if (arr[mid] > target) lo = mid + 1; 降序
if (arr[mid] < target) lo = mid + 1; //升序
else hi = mid;
}
if (arr[lo] == target){
return lo;
}
else return -1;
}
基本模版
private int binarySearch(int[] arr, int target){
int lo = 0, hi = arr.length - 1;
while (lo <= hi){ //注意这里是小于等于
int mid = lo + (hi - lo)/2;
if (arr[mid] == target) return mid;
if (arr[mid] < target){
lo = mid + 1;
}
else{
hi = mid - 1;
}
}
return -1;
}
附上全部的题解代码
/**
* // This is MountainArray's API interface.
* // You should not implement it, or speculate about its implementation
* interface MountainArray {
* public int get(int index) {}
* public int length() {}
* }
*/
class Solution {
public int findInMountainArray(int target, MountainArray mountainArr) {
// Find Moutain Peak
int lo = 0, hi = mountainArr.length()-1, peak = 0;
int size = hi;
if (size == -1) return -1;
while (lo < hi){
int mid = lo + (hi-lo)/2;
int mid_val = mountainArr.get(mid);
int right_val = mountainArr.get(mid+1);
if (mid_val < right_val) lo = mid + 1;
else hi = mid;
}
peak = lo;
if (mountainArr.get(peak) == target) return peak;
// Find on the left
lo = 0;
hi = peak - 1;
int left = binarySearch(mountainArr, target, lo, hi, true);
if (-1 != left) return left;
return binarySearch(mountainArr, target, peak+1, size, false);
}
private int binarySearch(MountainArray mountainArr, int target, int lo, int hi, boolean asc){
while (lo < hi){
int mid = lo + (hi - lo)/2;
if (asc){
if (mountainArr.get(mid) < target) lo = mid + 1;
else hi = mid;
}
else{
if (mountainArr.get(mid) > target) lo = mid + 1;
else hi = mid;
}
}
if (mountainArr.get(lo) == target) return lo; else return -1;
}
}