# """# 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}}}