A shop sells n kinds of items. price[i] is the regular price of one item of kind i, and needs[i] is how many of kind i you want to buy.
The shop also has bundle offers. Each row of special has n + 1 numbers: how many of each kind the bundle contains, then the bundle's price. You may use any bundle as many times as you like, but you must end up with exactly needs: a bundle that would give you an extra item cannot be used.
Return the lowest total you can pay for exactly the items you need.
Example 1
Input: price = [3, 4], special = [[2, 0, 5], [1, 1, 6]], needs = [3, 1] Output: 11 Why: one of each bundle (5 + 6) covers 3 of item 0 and 1 of item 1
2 more examples and the constraints
Example 2
Input: price = [2, 5], special = [[2, 2, 9]], needs = [1, 3] Output: 17 Why: the bundle has two of item 0 but only one is wanted: 2 + 3·5
Example 3
Input: price = [1, 1, 1], special = [[1, 1, 1, 4]], needs = [2, 2, 2] Output: 6 Why: the bundle costs more than its items, so skip it
Constraints
- 1 ≤ n ≤ 6
- Every price and every need is between 0 and 10
- There are 1 to 100 bundles
- Each bundle has n + 1 numbers between 0 and 50, and contains at least one item
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Try every bundle, recursivelyExponential
- Recursion with a memoO(S·m·n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 6 items, 10 of each, many bundles | Verdict |
|---|---|---|---|
| Try every bundle, recursively | Exponential | Correct, but repeats baskets | |
| Recursion with a memo | O(S·m·n) | Best; each basket once |
n = kinds of items, m = number of bundles, S = baskets reached (at most 11^n).
Edge cases to test
- Bundle bigger than the need. It can never be used, even if it is cheap:
[[2, 2, 9]]with needs[1, 3]. - Bundle dearer than its items. List price wins; starting
bestat list price handles it. - Needs all zero. The answer is 0 and no bundle fits.
- Free items. A price of 0 is allowed; list price still works as the fallback.
- Same bundle many times.
[[3, 20]]with price[10]and needs[9]uses it three times: 60.
Common mistakes
- Allowing a bundle that overshoots the need because it is cheaper.
- Forgetting the plain list-price option, so baskets no bundle fits come out wrong.
- Using a mutable list as a Python dict key, or an array as a Java map key, which fails or never matches.
How to talk through it
- 1Clarify. Can a bundle be used more than once? Is overbuying ever allowed? Can prices be 0?
- 2Define the state. The basket of remaining counts fully decides the cheapest price for the rest.
- 3Give the recursion. List price, or any fitting bundle plus the cost of what remains.
- 4Add the memo. Different bundle orders reach the same basket; cache by the counts.
- 5Test out loud. Run a bundle that overshoots, a bundle that is dearer than list, and needs of all zeros.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?