You are viewing a single comment's thread. Return to all 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
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 →