(This problem is an interactive problem.)
You may recall that an array arr
is a mountain array if and only if:
arr.length >= 3
i
with 0 < i < arr.length - 1
such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Given a mountain array mountainArr
, return the minimum index
such that mountainArr.get(index) == target
. If such an index
does not exist, return -1
.
You cannot 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 index k
(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: mountainArr = [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: mountainArr = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist in the array,
so we return -1.
Constraints:
3 <= mountainArr.length() <= 104
0 <= target <= 109
0 <= mountainArr.get(index) <= 109
This problem requires finding the minimum index of a target value within a mountain array using a limited number of calls to get()
(maximum 100). A mountain array is defined as an array that strictly increases until a peak, then strictly decreases. The solution leverages binary search efficiently.
Approach:
Find the Peak: First, we find the index of the peak element using binary search. We compare mountainArr.get(mid)
with mountainArr.get(mid + 1)
. If mountainArr.get(mid)
is greater, the peak is to the left; otherwise, it's to the right. This step requires approximately log₂(n)
calls to get()
.
Binary Search in Ascending and Descending Parts: Once we have the peak index, we perform two separate binary searches:
Return Minimum Index: If the target is found in either part, we return the minimum index. If not found in either part, we return -1.
Time Complexity Analysis:
Therefore, the overall time complexity is O(log n), which is very efficient. The space complexity is O(1), as we use only a constant amount of extra space.
Code Explanation (Python):
The Python code neatly encapsulates the approach.
findInMountainArray(target, mountain_arr)
: This is the main function. It first finds the peak index using binary search (while l < r
). Then, it uses a helper function search()
to perform binary search on both ascending and descending parts.
search(l, r, k)
: This helper function performs a modified binary search. The parameter k
is 1 for the ascending part and -1 for the descending part, making the comparison condition adapt accordingly.
The code in other languages follows the same structure and logic, only adapting the syntax to the specific language. All versions aim to minimize the calls to get()
to respect the constraint of less than 100 calls.