#1095

Find in Mountain Array

master · 1665 · lc hard +32 · verified · 41.2% accepted · 3,584 likes · top 21%

Description

(This is an interactive problem.)

An array arr is a mountain array if arr.length >= 3 and there exists an index i with 0 < i < arr.length - 1 such that:

- arr[0] < arr[1] < ... < arr[i]

- arr[i] > arr[i+1] > ... > arr[arr.length-1]

You have access to a MountainArray object only via:

- MountainArray.get(k) returns arr[k].

- MountainArray.length() returns the array length.

Using at most 100 calls to get, return the minimum index where mountainArr.get(index) == target, or -1 if target is not in the array.

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.

Code

1
2
3
4
5
6
7
8
9
10
11