You are viewing a single comment's thread. Return to all comments →
we can use binary search for the second array. lets assume that n > m then meaning, we would need to sort 2 arrays (O(nlogn)+O(mlogm) => 2O(nlogn))
then on each iteration of the keyboards we will do another log(n) operation
===> 2O(nlogn) + nlogn time, and O(1) space
def findMaxValue(arr, value) -> int: low,high= 0,(len(arr) - 1) while low <= high: mid = (low + high) // 2 if arr[mid] == value: return value elif arr[mid] > value: high = mid - 1 else: low = mid + 1 return None if arr[high] > value else arr[high]
maxValue = -1 keyboards.sort() drives.sort() for keyboard in keyboards: if keyboard >= b: continue maxDrive = findMaxValue(drives, b - keyboard) if maxDrive: maxValue = max(maxValue, keyboard + maxDrive) return maxValue
Seems like cookies are disabled on this browser, please enable them to open this website
Electronics Shop
You are viewing a single comment's thread. Return to all comments →
we can use binary search for the second array. lets assume that n > m then meaning, we would need to sort 2 arrays (O(nlogn)+O(mlogm) => 2O(nlogn))
then on each iteration of the keyboards we will do another log(n) operation
===> 2O(nlogn) + nlogn time, and O(1) space
def findMaxValue(arr, value) -> int: low,high= 0,(len(arr) - 1) while low <= high: mid = (low + high) // 2 if arr[mid] == value: return value elif arr[mid] > value: high = mid - 1 else: low = mid + 1 return None if arr[high] > value else arr[high]