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;
}
相关推荐
大明者省3 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
WWJA王文举4 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
白狐_7985 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander2585 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
Shell运维手记5 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
qq_448011166 小时前
C语言中的动态内存分配
c语言·开发语言·php
杨航 AI6 小时前
O(n log n):线性对数原理拆解 这个是排序算法的黄金复杂度之一。
算法·排序算法
船厂电气自动化ai大模型6 小时前
AI大模型与数学 第32课 函数凹凸性与二阶导数:拐点求解、凹凸区间计算(10道二阶导数计算题)
数据结构·人工智能·python·深度学习·算法
动词ing6 小时前
【学习笔记】C语言(数组指针与指针数组+字符数组+函数+参数传递+字符串作为形参+递归函数+指针函数+回调函数+结构体嵌套+内存动态分配函数)
c语言·笔记·学习
旖旎夜光6 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口