1095. Find in Mountain Array Hard

@problem@discussion
#Array#Binary Search#Interactive



1/**
2 * [1095] Find in Mountain Array
3 *
4 * (This problem is an interactive problem.)
5 * You may recall that an array arr is a mountain array if and only if:
6 * 
7 * 	arr.length >= 3
8 * 	There exists some i with 0 < i < arr.length - 1 such that:
9 * 	
10 * 		arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
11 * 		arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
12 * 	
13 * 	
14 * 
15 * Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.
16 * You cannot access the mountain array directly. You may only access the array using a MountainArray interface:
17 * 
18 * 	MountainArray.get(k) returns the element of the array at index k (0-indexed).
19 * 	MountainArray.length() returns the length of the array.
20 * 
21 * 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.
22 *  
23 * Example 1:
24 * 
25 * Input: array = [1,2,3,4,5,3,1], target = 3
26 * Output: 2
27 * Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
28 * Example 2:
29 * 
30 * Input: array = [0,1,2,4,2,1], target = 3
31 * Output: -1
32 * Explanation: 3 does not exist in the array, so we return -1.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	3 <= mountain_arr.length() <= 10^4
38 * 	0 <= target <= 10^9
39 * 	0 <= mountain_arr.get(index) <= 10^9
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/find-in-mountain-array/
45// discuss: https://leetcode.com/problems/find-in-mountain-array/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49/**
50 * // This is the MountainArray's API interface.
51 * // You should not implement it, or speculate about its implementation
52 *  struct MountainArray;
53 *  impl MountainArray {
54 *     fn get(index:i32)->i32;
55 *     fn length()->i32;
56 * };
57 */
58
59impl Solution {
60    pub fn find_in_mountain_array(target: i32, mountainArr: &MountainArray) -> i32 {
61        0
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_1095() {
73    }
74}
75


Back
© 2025 bowen.ge All Rights Reserved.