-2

EDIT: I've realized my question is a variant of the knapsack problem, only instead of maximum price and weight <= target, minimum price and length >= target. I'll try figuring it out and post an answer once I do.

Given an array of items, where each item has two properties - price and value, I need to find the cheapest combination (lowest total price) whose total value is greater than or equal to a target value.

An item may be chosen multiple times.

items = [{id:"a", v:10, p:8}, {id:"b", v:25, p:15}, {id:"c", v:45, p:20}]
target = 60
result = ["c","a"]

Any help would be greatly appreciated! Thanks!

animumina
  • 11
  • 4

1 Answers1

0

Here

# Use format [price,value]
data = [[12,10],[13,19],[12,13]]
tmp = []

for i in data:
    tmp.append(i[1]-i[0])

best_deal = max(tmp)
best_deal_locations = []
for i in range(len(tmp)):
    if tmp[i] == best_deal:
        best_deal_locations.append(data[i])

print('Best Deals Found:')
for i in best_deal_location:
    print('Price: ${}, Value: ${}'.format(i[0],i[1]))