[LeetCode] 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

山脉数组中查找目标值。

做这个题如果对山脉数组的定义不是很明确,可以先做一下852题。这个题可以跟162,852一起做。山脉数组基本上就是只有一个峰值 peak,峰值左边的元素是单调递增的,右边的元素是单调递减的。本题不允许你直接访问 input 数组,你只能通过 length() 函数知道数组的长度,通过 get() 函数得知某一个数字的值。只能用二分法做。

这个题的思路会用到三次二分法,第一次帮助找到 peak 元素,第二次去找 peak 元素左半边是否存在 target,第三次在 peak 元素右半边查找 target。

时间O(logn)

空间O(1)

Java实现

 1 /**
 2  * // This is MountainArray's API interface.
 3  * // You should not implement it, or speculate about its implementation
 4  * interface MountainArray {
 5  *     public int get(int index) {}
 6  *     public int length() {}
 7  * }
 8  */
 9 
10 class Solution {
11     public int findInMountainArray(int target, MountainArray mountainArr) {
12         int n = mountainArr.length();
13         int peak = 0;
14         int mid = 0;
15         // find index of peak
16         // 因为mid + 1可能会有越界的情况所以这里给了闭区间
17         // 下标都在index范围内
18         int left = 0;
19         int right = n - 1;
20         while (left < right) {
21             mid = left + (right - left) / 2;
22             if (mountainArr.get(mid) < mountainArr.get(mid + 1)) {
23                 left = peak = mid + 1;
24             } else {
25                 right = mid;
26             }
27         }
28 
29         // find target in the left of peak
30         left = 0;
31         right = peak;
32         while (left <= right) {
33             mid = left + (right - left) / 2;
34             if (mountainArr.get(mid) < target) {
35                 left = mid + 1;
36             } else if (mountainArr.get(mid) > target) {
37                 right = mid - 1;
38             } else {
39                 return mid;
40             }
41         }
42 
43         // find target in the right of peak
44         left = peak;
45         right = n - 1;
46         while (left <= right) {
47             mid = left + (right - left) / 2;
48             if (mountainArr.get(mid) > target) {
49                 left = mid + 1;
50             } else if (mountainArr.get(mid) < target) {
51                 right = mid - 1;
52             } else {
53                 return mid;
54             }
55         }
56         return -1;
57     }
58 }

相关题目

162. Find Peak Element

852. Peak Index in a Mountain Array

1095. Find in Mountain Array

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/12799822.html