Menu Minimum Cost
AirbnbOnsiteMedium
A menu sells bundles. Each bundle is a list of foods with one price, and you may buy any bundle any number of times. You want to end up with every food in a wanted list, and a bundle may hold extra foods you do not want; those are simply ignored.
Write cheapest_cover(bundles, prices, wanted), where bundles[i] lists the foods in bundle i and prices[i] is its price. Return [cost, chosen]: the lowest total price that gets every wanted food, and the indices of the bundles to buy, in increasing order. Each chosen bundle appears once.
Part 1: Cheapest cover of the wanted foods
For example, with bundles = [["a", "b", "c"], ["d", "e"], ["a", "c"]], prices = [5, 2, 1] and wanted = ["a", "c", "d"], the answer is [3, [1, 2]]: bundle 2 gives a and c for 1, and bundle 1 gives d for 2. Each chosen bundle appears once in the list.
- Example 1
- bundles
[["a","b","c"],["d","e"],["a","c"]]- prices
[5,2,1]- wanted
["a","c","d"]- Output
[3,[1,2]]
Why: Bundle 0 is not needed: bundle 2 (
a,c) costs 1 and bundle 1 (d,e) costs 2. The extraeis ignored. The cost is 3 and the bundles are 1 and 2. - Example 2
- bundles
[["burger"],["fries"],["burger","wings"]]- prices
[50,30,70]- wanted
["wings","fries"]- Output
[100,[1,2]]
Why:
wingsis only in bundle 2, which also holdsburger; that is fine, the extra food is ignored. Withfries(30) the cost is 100.
Constraints
1 <= len(wanted) <= 12- The foods in
wantedare distinct, and the foods inside one bundle are distinct 1 <= len(bundles) <= 100andlen(prices) == len(bundles)1 <= len(bundles[i]) <= 10and1 <= prices[i] <= 10^6- Foods are lowercase words of at most 12 letters or digits
- Every wanted food is in at least one bundle
- Exactly one set of bundles has the lowest total price