Sherlock and MiniMax Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defsherlockAndMinimax(arr,p,q):arr.sort()n=len(arr)# Evaluate the endpoints p and qcandidates=[p,q]# Evaluate midpoints between adjacent array elementsforiinrange(n-1):mid=(arr[i]+arr[i+1])// 2ifp<=mid<=q:candidates.append(mid)ifp<=mid+1<=q:candidates.append(mid+1)# Function to find the minimum distance to any array elementdefmin_distance(M):returnmin(abs(M-x)forxinarr)# Evaluate each candidate and keep track of the best onebest_M=Nonebest_min_distance=-1forMincandidates:current_min_distance=min_distance(M)if(current_min_distance>best_min_distance)or(current_min_distance==best_min_distanceand(best_MisNoneorM<best_M)):best_M=Mbest_min_distance=current_min_distancereturnbest_M
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and MiniMax
You are viewing a single comment's thread. Return to all comments →