Skip to content

Instantly share code, notes, and snippets.

@harshityadav95
Created June 9, 2021 14:31
Show Gist options
  • Save harshityadav95/6539e353a37f2aa0bb26716d49e1e0b3 to your computer and use it in GitHub Desktop.
Save harshityadav95/6539e353a37f2aa0bb26716d49e1e0b3 to your computer and use it in GitHub Desktop.
two-sum-less-than-k
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