The Key Forgot Capacity
items = [(2, 3), (3, 4)] # (weight, value)
memo = {}
def best(i, cap):
if i == len(items):
return 0
if i in memo: # cache keyed on i alone
return memo[i]
w, v = items[i]
take = 0
if w <= cap:
take = v + best(i + 1, cap - w)
res = max(take, best(i + 1, cap))
memo[i] = res
return res
print(best(0, 3))
What does this print, and what is the correct answer?
Sign in to answer questions and track your progress
Sign In