1095. 山脉数组中查找目标值
(这是一个 交互式问题 )
给你一个 山脉数组 mountainArr
,请你返回能够使得 mountainArr.get(index)
等于 target
最小 的下标 index
值。
如果不存在这样的下标 index
,就请返回 -1
。
何为山脉数组?如果数组 A
是一个山脉数组的话,那它满足如下条件:
首先,A.length >= 3
其次,在 0 < i < A.length - 1
条件下,存在 i
使得:
A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
你将 不能直接访问该山脉数组,必须通过 MountainArray
接口来获取数据:
MountainArray.get(k)
- 会返回数组中索引为k
的元素(下标从 0 开始)MountainArray.length()
- 会返回该数组的长度
注意:
对 MountainArray.get
发起超过 100
次调用的提交将被视为错误答案。此外,任何试图规避判题系统的解决方案都将会导致比赛资格被取消。
为了帮助大家更好地理解交互式问题,我们准备了一个样例 “答案”:https://leetcode.cn/playground/RKhe3ave,请注意这 不是一个正确答案。
示例 1:
输入:array = [1,2,3,4,5,3,1], target = 3 输出:2 解释:3 在数组中出现了两次,下标分别为 2 和 5,我们返回最小的下标 2。
示例 2:
输入:array = [0,1,2,4,2,1], target = 3 输出:-1 解释:3 在数组中没有出现,返回 -1。
提示:
3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9
原站题解
cpp 解法, 执行用时: 0 ms, 内存消耗: 7.1 MB, 提交时间: 2023-09-27 09:50:54
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class MountainArray { * public: * int get(int index); * int length(); * }; */ class Solution { int binary_search(MountainArray &mountain, int target, int l, int r, int key(int)) { target = key(target); while (l <= r) { int mid = (l + r) / 2; int cur = key(mountain.get(mid)); if (cur == target) { return mid; } else if (cur < target) { l = mid + 1; } else { r = mid - 1; } } return -1; } public: int findInMountainArray(int target, MountainArray &mountainArr) { int l = 0, r = mountainArr.length() - 1; while (l < r) { int mid = (l + r) / 2; if (mountainArr.get(mid) < mountainArr.get(mid + 1)) { l = mid + 1; } else { r = mid; } } int peak = l; int index = binary_search(mountainArr, target, 0, peak, [](int x) -> int{return x;}); if (index != -1) { return index; } return binary_search(mountainArr, target, peak + 1, mountainArr.length() - 1, [](int x) -> int{return -x;}); } };
java 解法, 执行用时: 0 ms, 内存消耗: 42 MB, 提交时间: 2023-09-27 09:50:06
/** * // 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) { int l = 0, r = mountainArr.length() - 1; while (l < r) { int mid = (l + r) / 2; if (mountainArr.get(mid) < mountainArr.get(mid + 1)) { l = mid + 1; } else { r = mid; } } int peak = l; int index = binarySearch(mountainArr, target, 0, peak, true); if (index != -1) { return index; } return binarySearch(mountainArr, target, peak + 1, mountainArr.length() - 1, false); } public int binarySearch(MountainArray mountainArr, int target, int l, int r, boolean flag) { if (!flag) { target *= -1; } while (l <= r) { int mid = (l + r) / 2; int cur = mountainArr.get(mid) * (flag ? 1 : -1); if (cur == target) { return mid; } else if (cur < target) { l = mid + 1; } else { r = mid - 1; } } return -1; } }
python3 解法, 执行用时: 52 ms, 内存消耗: 16.6 MB, 提交时间: 2023-09-27 09:49:47
# """ # This is MountainArray's API interface. # You should not implement it, or speculate about its implementation # """ #class MountainArray: # def get(self, index: int) -> int: # def length(self) -> int: def binary_search(mountain, target, l, r, key=lambda x: x): target = key(target) while l <= r: mid = (l + r) // 2 cur = key(mountain.get(mid)) if cur == target: return mid elif cur < target: l = mid + 1 else: r = mid - 1 return -1 class Solution: def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int: l, r = 0, mountain_arr.length() - 1 while l < r: mid = (l + r) // 2 if mountain_arr.get(mid) < mountain_arr.get(mid + 1): l = mid + 1 else: r = mid peak = l index = binary_search(mountain_arr, target, 0, peak) if index != -1: return index index = binary_search(mountain_arr, target, peak + 1, mountain_arr.length() - 1, lambda x: -x) return index
golang 解法, 执行用时: 4 ms, 内存消耗: 3.1 MB, 提交时间: 2023-09-27 09:47:18
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * type MountainArray struct { * } * * func (this *MountainArray) get(index int) int {} * func (this *MountainArray) length() int {} */ // 内置sort.Search 二分搜索 func findInMountainArray(target int, arr *MountainArray) int { n := arr.length() peak := sort.Search(n-1, func(i int) bool { return arr.get(i) >= arr.get(i+1) }) i := sort.Search(peak, func(i int) bool { return arr.get(i) >= target }) if arr.get(i) != target { i = peak + sort.Search(n-1-peak, func(i int) bool { return arr.get(peak+i) <= target }) if arr.get(i) != target { return -1 } } return i }