[Java]LeetCode1095. 山脉数组中查找目标值 | Find in Mountain Array
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11032162.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
(This problem is an interactive problem.)
You may recall that an array A
is a mountain array if and only if:
A.length >= 3
- There exists some
i
with0 < i < A.length - 1
such that:A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
Given a mountain array mountainArr
, return the minimum index
such that mountainArr.get(index) == target
. If such an index
doesn't exist, return -1
.
You can't access the mountain array directly. You may only access the array using a MountainArray
interface:
MountainArray.get(k)
returns the element of the array at indexk
(0-indexed).MountainArray.length()
returns the length of the array.
Submissions making more than 100
calls to MountainArray.get
will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.
Example 1:
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.
Example 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.
Constraints:
3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9
(这是一个 交互式问题 )
给你一个 山脉数组 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,请注意这 不是一个正确答案。
示例 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
1 /** 2 * // This is the MountainArray's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * class MountainArray { 5 * public: 6 * int get(int index); 7 * int length(); 8 * }; 9 */ 10 class Solution { 11 public: 12 int findInMountainArray(int target, MountainArray &mountainArr) { 13 int n = mountainArr.length(); 14 int low = 1, high = n - 1; 15 while (low < high) { 16 int mid = (low + high) / 2; 17 18 int a = mountainArr.get(mid - 1), b = mountainArr.get(mid), c = mountainArr.get(mid + 1); 19 20 if (a < b && b > c) { 21 low = high = mid; 22 break; 23 } 24 25 if (a < b && b < c) 26 low = mid + 1; 27 else if (a > b && b > c) 28 high = mid - 1; 29 else 30 assert(false); 31 } 32 33 int mountain = low; 34 35 low = 0; 36 high = mountain; 37 38 while (low < high) { 39 int mid = (low + high) / 2; 40 41 if (mountainArr.get(mid) >= target) 42 high = mid; 43 else 44 low = mid + 1; 45 } 46 47 if (mountainArr.get(low) == target) 48 return low; 49 50 low = mountain; 51 high = n - 1; 52 53 while (low < high) { 54 int mid = (low + high) / 2; 55 56 if (mountainArr.get(mid) <= target) 57 high = mid; 58 else 59 low = mid + 1; 60 } 61 62 if (mountainArr.get(low) == target) 63 return low; 64 65 return -1; 66 } 67 };
0ms
Java:
1 /** 2 * // This is MountainArray's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * interface MountainArray { 5 * public int get(int index) {} 6 * public int length() {} 7 * } 8 */ 9 10 class Solution { 11 int findInMountainArray(int target, MountainArray A) { 12 int n = A.length(), l, r, m, peak = 0; 13 // find index of peak 14 l = 0; 15 r = n - 1; 16 while (l < r) { 17 m = (l + r) / 2; 18 if (A.get(m) < A.get(m + 1)) 19 l = peak = m + 1; 20 else 21 r = m; 22 } 23 // find target in the left of peak 24 l = 0; 25 r = peak; 26 while (l <= r) { 27 m = (l + r) / 2; 28 if (A.get(m) < target) 29 l = m + 1; 30 else if (A.get(m) > target) 31 r = m - 1; 32 else 33 return m; 34 } 35 // find target in the right of peak 36 l = peak; 37 r = n - 1; 38 while (l <= r) { 39 m = (l + r) / 2; 40 if (A.get(m) > target) 41 l = m + 1; 42 else if (A.get(m) < target) 43 r = m - 1; 44 else 45 return m; 46 } 47 return -1; 48 } 49 }
24ms
Python3:
1 # """ 2 # This is MountainArray's API interface. 3 # You should not implement it, or speculate about its implementation 4 # """ 5 #class MountainArray: 6 # def get(self, index: int) -> int: 7 # def length(self) -> int: 8 9 class Solution: 10 def findInMountainArray(self, target: int, mo: 'MountainArray') -> int: 11 mlen = mo.length() 12 left, right, peak = 0, mlen - 1, 0 13 while left < right: 14 mid = left + (right - left) // 2 15 if mo.get(mid + 1) > mo.get(mid): 16 peak = mid + 1 17 left = mid + 1 18 else: 19 right = mid - 1 20 21 left, right = 0, peak 22 while left <= right: 23 mid = left + (right - left) // 2 24 val = mo.get(mid) 25 if val < target: 26 left = mid + 1 27 elif val == target: 28 return mid 29 else: 30 right = mid - 1 31 32 left, right = peak, mlen - 1 33 while left <= right: 34 mid = left + (right - left) // 2 35 val = mo.get(mid) 36 if val < target: 37 right = mid - 1 38 elif val == target: 39 return mid 40 else: 41 left = mid + 1 42 43 return -1