Skip to content

Latest commit

 

History

History

1095

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

(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 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: 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() <= 104
  • 0 <= target <= 109
  • 0 <= mountain_arr.get(index) <= 109

Companies: Bloomberg, Apple, Uber, Google

Related Topics:
Array, Binary Search, Interactive

Similar Questions:

Hints:

  • Based on whether A[i-1] < A[i] < A[i+1], A[i-1] < A[i] > A[i+1], or A[i-1] > A[i] > A[i+1], we are either at the left side, peak, or right side of the mountain. We can binary search to find the peak. After finding the peak, we can binary search two more times to find whether the value occurs on either side of the peak.

Solution 1. Binary Search

// OJ: https://leetcode.com/problems/find-in-mountain-array
// Author: github.com/lzl124631x
// Time: O(logN)
// Space: O(logN)
class Solution {
public:
    int findInMountainArray(int target, MountainArray &A) {
        int len = A.length();
        unordered_map<int, int> m;
        auto get = [&](int i) {
            return m.count(i) ? m[i] : (m[i] = A.get(i));
        };
        function<int(int, int, bool)> findInMonoArray;
        function<int(int, int)> findInMountainArray = [&](int L, int R) {
            if (L > R) return -1;
            if (L == R || L == R - 1) return get(L) == target ? L : (L == R - 1 && get(R) == target ? R : -1);
            int lv = get(L), rv = get(R), M = (L + R) / 2, a = get(M), b = get(M + 1);
            if (a < b) {
                int x = findInMonoArray(L, M + 1, true);
                return x != -1 ? x : findInMountainArray(M + 2, R);
            }
            int x = findInMountainArray(L, M - 1);
            return x != -1 ? x : findInMonoArray(M, R, false);
        };
        findInMonoArray = [&](int L, int R, bool increasing) {
            if (L > R) return -1;
            while (L <= R) {
                int M = (L + R) / 2, mid = get(M);
                if (mid == target) return M;
                if ((increasing && mid > target) || (!increasing && mid < target)) R = M - 1;
                else L = M + 1;
            }
            return -1;
        };
        return findInMountainArray(0, len - 1);
    }
};

Solution 2. Binary Search

// OJ: https://leetcode.com/problems/find-in-mountain-array/
// Author: github.com/lzl124631x
// Time: O(logN)
// Space: O(1)
class Solution {
    int binarySearch(int target, MountainArray &A, int L, int R, int dir) {
        while (L <= R) {
            int M = (L + R) / 2, val = A.get(M);
            if (val == target) return M;
            bool left = (dir == 1 && val < target) || (dir == -1 && val > target);
            if (left) L = M + 1;
            else R = M - 1;
        }
        return -1;
    }
    int findTop(MountainArray &A, int N) {
        int L = 0, R = N - 1;
        while (L <= R) {
            int M = (L + R) / 2, left = M - 1 >= 0 ? A.get(M - 1) : INT_MIN, val = A.get(M), right = M + 1 < N ? A.get(M + 1) : INT_MIN;
            if (val > left && val > right) return M;
            else if (val < left) R = M - 1;
            else L = M + 1;
        }
        return -1;
    }
public:
    int findInMountainArray(int target, MountainArray &A) {
        int N = A.length(), top = findTop(A, N), a = binarySearch(target, A, 0, top, 1);
        return a != -1 ? a : binarySearch(target, A, top + 1, N - 1, -1);
    }
};