Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1095. Find in Mountain Array #135

Open
Tcdian opened this issue Apr 29, 2020 · 1 comment
Open

1095. Find in Mountain Array #135

Tcdian opened this issue Apr 29, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Apr 29, 2020

1095. Find in Mountain Array

(这是一个 交互式问题 )

给你一个 山脉数组 mountainArr,请你返回能够使得 mountainArr.get(index) 等于 target 最小 的下标 index 值。

如果不存在这样的下标 index,就请返回 -1

 

何为山脉数组?如果数组 A 是一个山脉数组的话,那它满足如下条件:

首先A.length >= 3

其次,在 0 < i < A.length - 1 条件下,存在 i 使得:

A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
 

你将 不能直接访问该山脉数组,必须通过 MountainArray 接口来获取数据:

MountainArray.get(k) - 会返回数组中索引为k 的元素(下标从 0 开始)
MountainArray.length() - 会返回该数组的长度
 

注意:

对 MountainArray.get 发起超过 100 次调用的提交将被视为错误答案。此外,任何试图规避判题系统的解决方案都将会导致比赛资格被取消。

为了帮助大家更好地理解交互式问题,我们准备了一个样例 “答案”https://leetcode-cn.com/playground/RKhe3ave,请注意这 不是一个正确答案

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.

Note

  • 3 <= mountain_arr.length() <= 10000
  • 0 <= target <= 10^9
  • 0 <= mountain_arr.get(index) <= 10^9
@Tcdian
Copy link
Owner Author

Tcdian commented Apr 29, 2020

Solution

  • JavaScript Solution
/**
 * // This is the MountainArray's API interface.
 * // You should not implement it, or speculate about its implementation
 * function MountainArray() {
 *     @param {number} index
 *     @return {number}
 *     this.get = function(index) {
 *         ...
 *     };
 *
 *     @return {number}
 *     this.length = function() {
 *         ...
 *     };
 * };
 */

/**
 * @param {number} target
 * @param {MountainArray} mountainArr
 * @return {number}
 */
var findInMountainArray = function(target, mountainArr) {
    let len = mountainArr.length();
    let left = 0;
    let right = len - 1;
    let top;

    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        let midH = mountainArr.get(mid);
        let midLH = mountainArr.get(mid - 1);
        let midRH = mountainArr.get(mid + 1);
        if (mid === 0 || (midLH < midH && midH < midRH)) {
            left = mid + 1;
        } else if (mid === len - 1 || (midLH > midH && midH > midRH)) {
            right = mid - 1;
        } else {
            top = mid;
            break;
        }
    }

    left = 0;
    right = top;

    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        let midH = mountainArr.get(mid);
        if (midH === target) {
            return mid;
        } else if (midH < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    left = top;
    right = len - 1;

    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        let midH = mountainArr.get(mid);
        if (midH === target) {
            return mid;
        } else if (midH > target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return -1;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant