Interview Query

Filling Supermarket Bag

Start Timer

0:00:00

Upvote
1
Downvote
Save question
Mark as completed
View comments (6)
Next question

Let’s say you’re in a supermarket.

You’ve found out that there is a huge discount on some items you want; unfortunately, you only have one bag with a certain bag_capacity. Each of the items has its own value and capacity, stored in lists called values and capacities contained in a dictionary called items.

Write a function to pick the items that maximize the total value without exceeding the bag’s capacity and return the total value.

Note: You can’t take more than one copy of each item home. Also, you do not need to fill the entire bag to capacity.

Example:

Input:

items = {
  "values":[2,3,1,4],
  "capacities":[2, 4, 6, 5]
}

bag_capacity = 8

Output:

max_profit(items,bag_capacity) -> 6

We choose the first and last item with a total profit of 6 and a capacity of 7, which is less than the bag_capacity.

.
.
.
.
.


Comments

Loading comments