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;
}
相关推荐
To_OC8 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
2401_841495649 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
人邮异步社区13 小时前
怎么把C语言学到精通?
c语言·开发语言
爱刷碗的苏泓舒13 小时前
PPP-AR 中的参考星选取:数学原理、评价指标与切换处理
算法·gnss·模糊度固定·ppp-ar·星间单差·参考星·卫星端偏差
烬羽16 小时前
递归老写崩?一个"退回"公式,把回溯题变成填空题
javascript·深度学习·算法
闪电悠米16 小时前
力扣hot100-41.缺失的第一个正数-原地哈希详解
数据结构·算法·哈希算法
星空露珠16 小时前
28种颜色对应名称,
开发语言·数据库·算法·游戏·lua
xiaobobo333018 小时前
C语言中宏定义宏名和宏值之间的关系
c语言·宏定义·语义型
QXWZ_IA18 小时前
桥梁数字孪生怎么落地?
人工智能·科技·算法·智能硬件·政务