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;
}
相关推荐
不会就选b2 小时前
算法日常・每日刷题--<二分查找>1
算法
「維他檸檬茶」2 小时前
大模型算法学习2026.6.13
学习·算法
叫我:松哥2 小时前
基于Python的共享单车租赁数据分析与预测系统,技术栈flask+boostrap+随机森林+XGBoost
人工智能·python·深度学习·算法·随机森林·数据分析·flask
BAGAE2 小时前
星链卫星数据获取:从太空安全到实时通信的技术革命
网络·数据结构·数据库·算法·云计算·hbase
happymaker06262 小时前
LeetCodeHor100——438.找到字符串中所有的字母异位词
算法
西安邮电大学2 小时前
有关栈的经典算法题
java·后端·其他·算法·面试
h_a_o777oah3 小时前
【算法专项】扩展域并查集:原理详解及解决大部分种类并查集问题(洛谷P5937 P2024 C++代码)
数据结构·c++·算法·acm·并查集·扩展域·逻辑建模
兰令水3 小时前
leecodecode【单调栈】【2026.6.12打卡-java版本】
java·开发语言·算法
dnbug Blog3 小时前
C程序 基本语法
c语言·基本语法