1095. Find in Mountain Array
Problem
(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
Discussion
A complex version of 852. Peak Index in a Mountain Array. We have to locate the target value from a given mountain array.
In order to perform binary search, we first find the peak index in the input. Then we simply perform binary search on the rising-side then falling-side of the input.
Solution
We first find the peak index with a separate function
findPeakIndexInMountainArray
, which is exactly the implementation of
852. Peak Index in a Mountain Array.
Then we perform binary search on the rising-side of the array, then the falling-side. Note that in each binary search, we have to check if the ending element is target, as it's missed in the binary search scanning process.
# """
# 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:
class Solution:
def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
# location peak
peak = self.findPeakIndexInMountainArray(mountain_arr)
# searching in the rising side
head, tail = 0, peak
while head < tail:
mid = (head + tail) // 2
if mountain_arr.get(mid) == target:
return mid
elif mountain_arr.get(mid) < target:
head = mid + 1
else:
tail = mid
if mountain_arr.get(tail) == target:
return tail
# searching in the falling side
head, tail = peak, mountain_arr.length() - 1
while head < tail:
mid = (head + tail) // 2
if mountain_arr.get(mid) == target:
return mid
elif mountain_arr.get(mid) > target:
head = mid + 1
else:
tail = mid
if mountain_arr.get(tail) == target:
return tail
return -1
def findPeakIndexInMountainArray(self, mountain_arr: 'MountainArray') -> int:
head, tail = 0, mountain_arr.length() - 1
while head < tail:
mid = (head + tail) // 2
if mountain_arr.get(mid) < mountain_arr.get(mid + 1):
head = mid + 1
else:
tail = mid
return head
Complexity Analysis
- Time Complexity:
O(log(n))
, as we perform standard binary search three times. - Space Complexity:
O(1)
, as we only perform binary search and saving constant values.