Created
June 9, 2021 14:31
-
-
Save harshityadav95/6539e353a37f2aa0bb26716d49e1e0b3 to your computer and use it in GitHub Desktop.
two-sum-less-than-k
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def searchIndex(self,arr,target): | |
low,high=0,len(arr)-1 | |
while low<=high: | |
mid=low+int((high-low)/2) | |
if arr[mid][1]>target: | |
high=mid-1 | |
else: | |
low=mid+1 | |
return max(0,low-1) | |
def optimalUtilization(self,A,B,target): | |
B=sorted(B,key=lambda x:x[1]) | |
n=len(A) | |
arr=[] | |
maxx=0 | |
for i in range(n): | |
index=self.searchIndex(B,target-A[i][1]) | |
if A[i][1]+B[index][1]<=target and A[i][1]+B[index][1]>maxx: | |
maxx=A[i][1]+B[index][1] | |
arr=[[A[i][0],B[index][0]]] | |
elif A[i][1]+B[index][1]==maxx: | |
arr.append([A[i][0],B[index][0]]) | |
return arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment