LeetCode //C - 1095. Find in Mountain Array

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:
    • arr0 < arr1 < ... < arri - 1 < arri
    • arri > arri + 1 > ... > arrarr.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:
c 复制代码
/**
 * *********************************************************************
 * // 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;
}
相关推荐
zhiSiBuYu05171 小时前
重排序(Rerank)提升检索准确率实战指南
开发语言·python·算法
月疯1 小时前
华为手环的部分功能
算法
郭梧悠1 小时前
算法:有效的括号
python·算法·leetcode
atunet1 小时前
关于算法设计模式的演化与编程范式变迁的技术7
算法·设计模式
Jerry1 小时前
LeetCode 27. 移除元素
算法
旖-旎1 小时前
《LeetCode 1137 第N个泰波那契数 和 LeetCode 三步问题》
c++·算法·leetcode·动态规划
wabs6662 小时前
关于动态规划【力扣718.最长重复子数组的思考】
算法·leetcode·动态规划
技术小黑2 小时前
CNN算法实战系列08 | ResNeXt-50算法实战与猴痘病识别
人工智能·算法·cnn
Full Stack Developme2 小时前
Java 漏斗算法 及应用场景
java·开发语言·算法
zhangfeng11332 小时前
算子开发 Overwrite 覆盖/替换模式 Accumulate 累加模式,性能对比 memset错误 bat_alloc 错误
c语言·人工智能·gnu·算子开发