1095. Find in Mountain Array

(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: 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 < = m o u n t a i n A r r . l e n g t h ( ) < = 10 4 3 <= mountainArr.length() <= 10^4 3<=mountainArr.length()<=104
  • 0 < = t a r g e t < = 10 9 0 <= target <= 10^9 0<=target<=109
  • 0 < = m o u n t a i n A r r . g e t ( i n d e x ) < = 10 9 0 <= mountainArr.get(index) <= 10^9 0<=mountainArr.get(index)<=109

From: LeetCode
Link: 1095. Find in Mountain Array


Solution:

Ideas:

find the peak first, then search the left increasing side before the right decreasing side, so the first found index is the minimum index.

Code:
/**
 * *********************************************************************
 * // This is the MountainArray's API interface.
 * // You should not implement it, or speculate about its implementation
 * *********************************************************************
 *
 * int get(MountainArray *, int index);
 * int length(MountainArray *);
 */

int findInMountainArray(int target, MountainArray* mountainArr) {
    int n = length(mountainArr);

    /* 1. Find peak index */
    int left = 0, right = n - 1;

    while (left < right) {
        int mid = left + (right - left) / 2;

        if (get(mountainArr, mid) < get(mountainArr, mid + 1)) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }

    int peak = left;

    /* 2. Binary search in increasing part */
    left = 0;
    right = peak;

    while (left <= right) {
        int mid = left + (right - left) / 2;
        int val = get(mountainArr, mid);

        if (val == target) {
            return mid;
        } else if (val < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    /* 3. Binary search in decreasing part */
    left = peak + 1;
    right = n - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;
        int val = get(mountainArr, mid);

        if (val == target) {
            return mid;
        } else if (val < target) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }

    return -1;
}
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐