• + 0 comments
        """
        An optimized version. Instead of checking all combinations, we sort both lists in descending order
        and break the inner loop when b - total cannot improve."""
    		
    def getMoneySpent(keyboards, drives, b):
        keyboards.sort(reverse = True)
        drives.sort(reverse = True)
        max_spent = -1
        diff = b 
        for k in keyboards:
            for d in drives:
                total = k + d
                if b - total >= 0:
                    if b - total < diff:
                        diff = b - total
                        max_spent = total
                    else :
                        break
        return max_spent