山脉数组中查找目标值

1095. Find in Mountain Array

来源: LeetCode 1095. Find in Mountain Array

题目描述

1095. Find in Mountain Array
(This problem is an interactive problem.)

You may recall that an array A is a mountain array if and only if:

A.length >= 3
There exists some i with 0 < i < A.length - 1 such that:
A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target.  If such an index doesn't exist, return -1.

You can't 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() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9

思路分析

仅允许100次call
肯定二分啊
先找到峰值然后分别处理左右
感觉不算hard题啊,就medium吧
可能有更牛逼的解法吧
这个操作下最多80次call

代码

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

class Solution {
public:
    
    int findInMountainArray(int target, MountainArray &mountainArr) {
        // only can get 100 calls
        // 最大数据量10000 
        int top = mountainArr.length();
        int i = 0, j = top-1;
        while(i+1<j){
            int mid = i + ((j-i)>>1);
            int a = mountainArr.get(mid-1);
            int b = mountainArr.get(mid+1);
            int c = mountainArr.get(mid);
            if(a<c && b<c){
                top = mid;
                break;
            } // 
            else if(a<c) i = mid; // up
            else j = mid; // down
        } // find top max 14 * 3 = 42
        int res = min(fRecur(mountainArr, target, 0, top, true), fRecur(mountainArr, target, top+1, mountainArr.length() - 1, false)); // 最坏 13 + 13 = 26
        return res == INT_MAX ? -1 : res;
    }
    int fRecur(MountainArray &mountainArr, int target, int l, int r, bool tag){
        auto& m = mountainArr; 
        while(l<=r){
            int mid = l +((r-l)>>1);
            int a = m.get(mid);
            if(a<target) {
                if(tag) l = mid + 1;
                else r = mid - 1;
            }
            else if(a > target){
                if(tag) r = mid - 1;
                else l = mid + 1;
            }
            else return mid;
        }
        return INT_MAX;
    }
};

算法分析

- 时间复杂度:O(lgn)
- 空间复杂度:O(1)

代码改进

看了下其他人的真就只有二分查找呗,那这道题确实不够hard啊
可以改进下下面的如果前面找到后面就不用找了
/**
 * // This is the MountainArray's API interface.
 * // You should not implement it, or speculate about its implementation
 * class MountainArray {
 *   public:
 *     int get(int index);
 *     int length();
 * };
 */

class Solution {
public:
    
    int findInMountainArray(int target, MountainArray &mountainArr) {
        // only can get 100 calls
        // 最大数据量10000 
        int top = mountainArr.length();
        int i = 0, j = top-1;
        while(i+1<j){
            int mid = i + ((j-i)>>1);
            int a = mountainArr.get(mid-1);
            int b = mountainArr.get(mid+1);
            int c = mountainArr.get(mid);
            if(a<c && b<c){
                top = mid;
                break;
            } // 
            else if(a<c) i = mid; // up
            else j = mid; // down
        } // find top max 14 * 3 = 42
        int res =  fRecur(mountainArr, target, 0, top, true);
        if(res == INT_MAX) res = fRecur(mountainArr, target, top+1, mountainArr.length() - 1, false); // 最坏 13 + 13 = 26
        return res == INT_MAX ? -1 : res;
    }
    int fRecur(MountainArray &mountainArr, int target, int l, int r, bool tag){
        auto& m = mountainArr; 
        while(l<=r){
            int mid = l +((r-l)>>1);
            int a = m.get(mid);
            if(a<target) {
                if(tag) l = mid + 1;
                else r = mid - 1;
            }
            else if(a > target){
                if(tag) r = mid - 1;
                else l = mid + 1;
            }
            else return mid;
        }
        return INT_MAX;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值