We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(这是一个 交互式问题 )
给你一个 山脉数组 mountainArr,请你返回能够使得 mountainArr.get(index) 等于 target 最小 的下标 index 值。
mountainArr
mountainArr.get(index)
target
index
如果不存在这样的下标 index,就请返回 -1。
-1
何为山脉数组?如果数组 A 是一个山脉数组的话,那它满足如下条件:
首先,A.length >= 3
A.length >= 3
其次,在 0 < i < A.length - 1 条件下,存在 i 使得:
0 < i < A.length - 1
i
A[0] < A[1] < ... A[i-1] < A[i] A[i] > A[i+1] > ... > A[A.length - 1]
A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
你将 不能直接访问该山脉数组,必须通过 MountainArray 接口来获取数据:
MountainArray
MountainArray.get(k) - 会返回数组中索引为k 的元素(下标从 0 开始) MountainArray.length() - 会返回该数组的长度
MountainArray.get(k)
MountainArray.length()
注意:
对 MountainArray.get 发起超过 100 次调用的提交将被视为错误答案。此外,任何试图规避判题系统的解决方案都将会导致比赛资格被取消。
MountainArray.get
为了帮助大家更好地理解交互式问题,我们准备了一个样例 “答案”:https://leetcode-cn.com/playground/RKhe3ave,请注意这 不是一个正确答案。
Input: array = [1,2,3,4,5,3,1], target = 3 Output: 2 Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
Input: array = [0,1,2,4,2,1], target = 3 Output: -1 Explanation: 3 does not exist in the array, so we return -1.
3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9
The text was updated successfully, but these errors were encountered:
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * function MountainArray() { * @param {number} index * @return {number} * this.get = function(index) { * ... * }; * * @return {number} * this.length = function() { * ... * }; * }; */ /** * @param {number} target * @param {MountainArray} mountainArr * @return {number} */ var findInMountainArray = function(target, mountainArr) { let len = mountainArr.length(); let left = 0; let right = len - 1; let top; while (left <= right) { const mid = Math.floor((left + right) / 2); let midH = mountainArr.get(mid); let midLH = mountainArr.get(mid - 1); let midRH = mountainArr.get(mid + 1); if (mid === 0 || (midLH < midH && midH < midRH)) { left = mid + 1; } else if (mid === len - 1 || (midLH > midH && midH > midRH)) { right = mid - 1; } else { top = mid; break; } } left = 0; right = top; while (left <= right) { const mid = Math.floor((left + right) / 2); let midH = mountainArr.get(mid); if (midH === target) { return mid; } else if (midH < target) { left = mid + 1; } else { right = mid - 1; } } left = top; right = len - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); let midH = mountainArr.get(mid); if (midH === target) { return mid; } else if (midH > target) { left = mid + 1; } else { right = mid - 1; } } return -1; };
Sorry, something went wrong.
No branches or pull requests
1095. Find in Mountain Array
(这是一个 交互式问题 )
给你一个 山脉数组
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.com/playground/RKhe3ave,请注意这 不是一个正确答案。
Example 1
Example 2
Note
3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9
The text was updated successfully, but these errors were encountered: