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:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Given a mountain array mountainArr, return the minimumindex 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: 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.
# """# This is MountainArray's API interface.# You should not implement it, or speculate about its implementation# """# class MountainArray:# def get(self, index: int) -> int:# def length(self) -> int:classSolution:deffindInMountainArray(self,target:int,mountain_arr:'MountainArray')->int:defsearch(l:int,r:int,k:int)->int:whilel<r:mid=(l+r)>>1ifk*mountain_arr.get(mid)>=k*target:r=midelse:l=mid+1return-1ifmountain_arr.get(l)!=targetelseln=mountain_arr.length()l,r=0,n-1whilel<r:mid=(l+r)>>1ifmountain_arr.get(mid)>mountain_arr.get(mid+1):r=midelse:l=mid+1ans=search(0,l,1)returnsearch(l+1,n-1,-1)ifans==-1elseans
/** * // This is MountainArray's API interface. * // You should not implement it, or speculate about its implementation * interface MountainArray { * public int get(int index) {} * public int length() {} * } */classSolution{privateMountainArraymountainArr;privateinttarget;publicintfindInMountainArray(inttarget,MountainArraymountainArr){intn=mountainArr.length();intl=0,r=n-1;while(l<r){intmid=(l+r)>>>1;if(mountainArr.get(mid)>mountainArr.get(mid+1)){r=mid;}else{l=mid+1;}}this.mountainArr=mountainArr;this.target=target;intans=search(0,l,1);returnans==-1?search(l+1,n-1,-1):ans;}privateintsearch(intl,intr,intk){while(l<r){intmid=(l+r)>>>1;if(k*mountainArr.get(mid)>=k*target){r=mid;}else{l=mid+1;}}returnmountainArr.get(l)==target?l:-1;}}
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class MountainArray { * public: * int get(int index); * int length(); * }; */classSolution{public:intfindInMountainArray(inttarget,MountainArray&mountainArr){intn=mountainArr.length();intl=0,r=n-1;while(l<r){intmid=(l+r)>>1;if(mountainArr.get(mid)>mountainArr.get(mid+1)){r=mid;}else{l=mid+1;}}autosearch=[&](intl,intr,intk)->int{while(l<r){intmid=(l+r)>>1;if(k*mountainArr.get(mid)>=k*target){r=mid;}else{l=mid+1;}}returnmountainArr.get(l)==target?l:-1;};intans=search(0,l,1);returnans==-1?search(l+1,n-1,-1):ans;}};
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * type MountainArray struct { * } * * func (this *MountainArray) get(index int) int {} * func (this *MountainArray) length() int {} */funcfindInMountainArray(targetint,mountainArr*MountainArray)int{n:=mountainArr.length()l,r:=0,n-1forl<r{mid:=(l+r)>>1ifmountainArr.get(mid)>mountainArr.get(mid+1){r=mid}else{l=mid+1}}search:=func(l,r,kint)int{forl<r{mid:=(l+r)>>1ifk*mountainArr.get(mid)>=k*target{r=mid}else{l=mid+1}}ifmountainArr.get(l)==target{returnl}return-1}ans:=search(0,l,1)ifans==-1{returnsearch(l+1,n-1,-1)}returnans}
/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class Master { * get(index: number): number {} * * length(): number {} * } */functionfindInMountainArray(target:number,mountainArr:MountainArray){constn=mountainArr.length();letl=0;letr=n-1;while(l<r){constmid=(l+r)>>1;if(mountainArr.get(mid)>mountainArr.get(mid+1)){r=mid;}else{l=mid+1;}}constsearch=(l:number,r:number,k:number):number=>{while(l<r){constmid=(l+r)>>1;if(k*mountainArr.get(mid)>=k*target){r=mid;}else{l=mid+1;}}returnmountainArr.get(l)===target?l:-1;};constans=search(0,l,1);returnans===-1?search(l+1,n-1,-1):ans;}
implSolution{#[allow(dead_code)]pubfnfind_in_mountain_array(target:i32,mountain_arr:&MountainArray)->i32{letn=mountain_arr.length();// First find the maximum element in the arrayletmutl=0;letmutr=n-1;whilel<r{letmid=(l+r)>>1;ifmountain_arr.get(mid)>mountain_arr.get(mid+1){r=mid;}else{l=mid+1;}}letleft=Self::binary_search(mountain_arr,0,l,1,target);ifleft==-1{Self::binary_search(mountain_arr,l,n-1,-1,target)}else{left}}#[allow(dead_code)]fnbinary_search(m:&MountainArray,mutl:i32,mutr:i32,k:i32,target:i32)->i32{letn=m.length();whilel<r{letmid=(l+r)>>1;ifk*m.get(mid)>=k*target{r=mid;}else{l=mid+1;}}ifm.get(l)==target{l}else{-1}}}