• + 0 comments

    Two-pointer solution:

        keyboards.sort()
        drives.sort(reverse=True)
        
        best = -1
        i = j = 0
        
        while i < len(keyboards) and j < len(drives):
            total = keyboards[i] + drives[j]
            if total > b:
                j += 1
            else:
                if total > best:
                    best = total
                i += 1
        return best