Find in Mountain Array Problem
Description
LeetCode Problem 1095.
(This problem is an interactive problem.)
You may recall that an array arr is a mountain array if and only if:
arr.length >= 3- There exists some
iwith0 < i < arr.length - 1such 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 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: 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() <= 10^40 <= target <= 10^90 <= mountainArr.get(index) <= 10^9
Solution
Approach: Three Binary Searches
With at most 100 calls to get and up to 104 elements, a linear scan is not allowed; everything has to be a binary search (each one costs about log2(104) ≈ 14 steps).
- Find the peak. Look at
get(mid)andget(mid + 1). If the next element is smaller,midis on the falling slope or is the peak itself, so the peak is atmidor to its left. Otherwise we are still climbing and the peak is to the right ofmid. This narrows down to the peak index. - Search the rising slope
0 .. peak, which is sorted in increasing order, for the first index with value>= target, then check whether the value there istarget. - If it was not found, search the falling slope
peak + 1 .. n - 1, which is sorted in decreasing order.
The order matters: target can appear once on each slope, and the problem asks for the smaller index, which is always the one on the rising slope. So only fall back to the right side when the left side has no match.
Both slope searches can share one function: multiply the values and the target by sign = 1 on the rising slope and sign = -1 on the falling slope. That turns the falling slope into an increasing one, and the same lower-bound search works on both.
This uses about 2 · 14 calls for the peak and 15 for each slope, well under 100.
Complexity: O(log n) time and calls to get, O(1) extra space.
# """
# 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, mountainArr: 'MountainArray') -> int:
n = mountainArr.length()
# 1. Find the peak: the first index whose next element is smaller.
lo, hi = 0, n - 1
while lo < hi:
mid = (lo + hi) // 2
if mountainArr.get(mid) > mountainArr.get(mid + 1):
hi = mid
else:
lo = mid + 1
peak = lo
# 2. Binary search one slope. sign = 1 on the rising slope, -1 on the falling one,
# so that sign * value is increasing on both.
def search(lo: int, hi: int, sign: int) -> int:
while lo < hi:
mid = (lo + hi) // 2
if sign * mountainArr.get(mid) >= sign * target:
hi = mid
else:
lo = mid + 1
return lo if mountainArr.get(lo) == target else -1
# 3. The rising slope first: its indexes are the smaller ones.
index = search(0, peak, 1)
return index if index != -1 else search(peak + 1, n - 1, -1)