An array arr is a mountain array if and only if:
-
arr.length >= 3
-
There exists some
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 mountain_arr, return the minimum index such that mountain_arr.get(index) == target. If no such index exists, return -1.
You cannot access the mountain array directly. You may only read it through a MountainArray interface, and a solution that makes more than 100 calls to get is judged wrong.
Function Signature
def find_in_mountain_array(target: int, mountain_arr: "MountainArray") -> int:
Rules
-
mountain_arr
is an object with two methods.
mountain_arr.get(k)
returns the element at index
k
(0-indexed).
mountain_arr.length()
returns the length of the array.
-
More than 100 calls to
mountain_arr.get
make the answer wrong, even if the returned index is correct.
-
The input is always a valid mountain array. Because each side of the peak is strictly monotonic, a value appears at most once on each side, so it appears at most twice in total. When it appears twice, return the smaller index.
-
In the examples,
mountain_arr
is written as the list of values it holds.
Constraints
-
3 <= mountain_arr.length() <= 10^4
-
0 <= target <= 10^9
-
0 <= mountain_arr.get(index) <= 10^9
Examples
Input: target = 3, mountain_arr = [1,2,3,4,5,3,1]
Output: 2
The value 3 appears at index 2 on the rising side and at index 5 on the falling side. The minimum index is 2.
Input: target = 3, mountain_arr = [0,1,2,4,2,1]
Output: -1
No element equals 3, so the answer is -1.
Input: target = 4, mountain_arr = [1,3,5,4,0]
Output: 3
The rising side holds 1, 3 and 5, so 4 is not there. The only match is on the falling side, at index 3.