Airbnb questions

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.

  1. 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 extra e is ignored. The cost is 3 and the bundles are 1 and 2.

  2. Example 2
    bundles
    [["burger"],["fries"],["burger","wings"]]
    prices
    [50,30,70]
    wanted
    ["wings","fries"]
    Output
    [100,[1,2]]

    Why: wings is only in bundle 2, which also holds burger; that is fine, the extra food is ignored. With fries (30) the cost is 100.

Constraints

  • 1 <= len(wanted) <= 12
  • The foods in wanted are distinct, and the foods inside one bundle are distinct
  • 1 <= len(bundles) <= 100 and len(prices) == len(bundles)
  • 1 <= len(bundles[i]) <= 10 and 1 <= 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
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question