[Java]LeetCode1095. 山脈數組中查找目標值 | Find in Mountain Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-upkogilh-ma.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

(This problem is an interactive problem.)git

You may recall that an array A is a mountain array if and only if:github

  • 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.spa

Example 1:code

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:htm

Input: array = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist in  so we return -1.the array,

Constraints:blog

  1. 3 <= mountain_arr.length() <= 10000
  2. 0 <= target <= 10^9
  3. 0 <= mountain_arr.get(index) <= 10^9

(這是一個 交互式問題 )索引

給你一個 山脈數組 mountainArr,請你返回可以使得 mountainArr.get(index) 等於 target 最小 的下標 index 值。

若是不存在這樣的下標 index,就請返回 -1

所謂山脈數組,即數組 A 假如是一個山脈數組的話,須要知足以下條件:

首先,A.length >= 3

其次,在 0 < i < A.length - 1 條件下,存在 i 使得:

  • A[0] < A[1] < ... A[i-1] < A[i]
  • A[i] > A[i+1] > ... > A[A.length - 1]

你將 不能直接訪問該山脈數組,必須經過 MountainArray 接口來獲取數據:

  • MountainArray.get(k) - 會返回數組中索引爲k 的元素(下標從 0 開始)
  • MountainArray.length() - 會返回該數組的長度

注意:

對 MountainArray.get 發起超過 100 次調用的提交將被視爲錯誤答案。此外,任何試圖規避判題系統的解決方案都將會致使比賽資格被取消。

爲了幫助你們更好地理解交互式問題,咱們準備了一個樣例 「答案」:https://leetcode-cn.com/playground/RKhe3ave,請注意這 不是一個正確答案。

示例 1:

輸入:array = [1,2,3,4,5,3,1], target = 3
輸出:2
解釋:3 在數組中出現了兩次,下標分別爲 2 和 5,咱們返回最小的下標 2。

示例 2:

輸入:array = [0,1,2,4,2,1], target = 3
輸出:-1
解釋:3 在數組中沒有出現,返回 -1。

提示:

  1. 3 <= mountain_arr.length() <= 10000
  2. 0 <= target <= 10^9
  3. 0 <= mountain_arr.get(index) <= 10^9

Runtime: 0 ms
C++:
 1 /**
 2  * // This is the MountainArray's API interface.
 3  * // You should not implement it, or speculate about its implementation
 4  * class MountainArray {
 5  *   public:
 6  *     int get(int index);
 7  *     int length();
 8  * };
 9  */
10 class Solution {
11 public:
12     int findInMountainArray(int target, MountainArray &mountainArr) {
13         int n = mountainArr.length();
14         int low = 1, high = n - 1;
15         while (low < high) {
16             int mid = (low + high) / 2;
17 
18             int a = mountainArr.get(mid - 1), b = mountainArr.get(mid), c = mountainArr.get(mid + 1);
19 
20             if (a < b && b > c) {
21                 low = high = mid;
22                 break;
23             }
24 
25             if (a < b && b < c)
26                 low = mid + 1;
27             else if (a > b && b > c)
28                 high = mid - 1;
29             else
30                 assert(false);
31         }
32 
33         int mountain = low;
34 
35         low = 0;
36         high = mountain;
37 
38         while (low < high) {
39             int mid = (low + high) / 2;
40 
41             if (mountainArr.get(mid) >= target)
42                 high = mid;
43             else
44                 low = mid + 1;
45         }
46 
47         if (mountainArr.get(low) == target)
48             return low;
49 
50         low = mountain;
51         high = n - 1;
52 
53         while (low < high) {
54             int mid = (low + high) / 2;
55 
56             if (mountainArr.get(mid) <= target)
57                 high = mid;
58             else
59                 low = mid + 1;
60         }
61 
62         if (mountainArr.get(low) == target)
63             return low;
64 
65         return -1;
66     }
67 };

0ms

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        int findInMountainArray(int target, MountainArray A) {
12         int n = A.length(), l, r, m, peak = 0;
13         // find index of peak
14         l  = 0;
15         r = n - 1;
16         while (l < r) {
17             m = (l + r) / 2;
18             if (A.get(m) < A.get(m + 1))
19                 l = peak = m + 1;
20             else
21                 r = m;
22         }
23         // find target in the left of peak
24         l = 0;
25         r = peak;
26         while (l <= r) {
27             m = (l + r) / 2;
28             if (A.get(m) < target)
29                 l = m + 1;
30             else if (A.get(m) > target)
31                 r = m - 1;
32             else
33                 return m;
34         }
35         // find target in the right of peak
36         l = peak;
37         r = n - 1;
38         while (l <= r) {
39             m = (l + r) / 2;
40             if (A.get(m) > target)
41                 l = m + 1;
42             else if (A.get(m) < target)
43                 r = m - 1;
44             else
45                 return m;
46         }
47         return -1;
48     }
49 }

24ms

Python3:

 1 # """
 2 # This is MountainArray's API interface.
 3 # You should not implement it, or speculate about its implementation
 4 # """
 5 #class MountainArray:
 6 #    def get(self, index: int) -> int:
 7 #    def length(self) -> int:
 8 
 9 class Solution:
10     def findInMountainArray(self, target: int, mo: 'MountainArray') -> int:
11         mlen = mo.length()
12         left, right, peak = 0, mlen - 1, 0
13         while left < right:
14             mid = left + (right - left) // 2
15             if mo.get(mid + 1) > mo.get(mid):
16                 peak = mid + 1
17                 left = mid + 1
18             else:
19                 right = mid - 1
20         
21         left, right = 0, peak
22         while left <= right:
23             mid = left + (right - left) // 2
24             val = mo.get(mid)
25             if val < target:
26                 left = mid + 1
27             elif val == target:
28                 return mid
29             else:
30                 right = mid - 1
31 
32         left, right = peak, mlen - 1
33         while left <= right:
34             mid = left + (right - left) // 2
35             val = mo.get(mid)
36             if val < target:
37                 right = mid - 1
38             elif val == target:
39                 return mid
40             else:
41                 left = mid + 1
42                 
43         return -1
相關文章
相關標籤/搜索