635. Bundle Shopping
A shop sells n kinds of items. price[i] is the price of one item of kind i. The shop also has bundles: each row of specials has n + 1 numbers, where the first n are how many items of each kind the bundle contains and the last one is the price of the whole bundle. Any bundle can be bought as often as you like.
You must buy at least needs[i] items of each kind i, but you are not allowed to buy more than you need (a bundle that would give more of some kind than still required cannot be used). Bundles that contain no items at all are ignored. Return the lowest total price.
Example 1
- Input:
- price = [4,7]specials = [[1,1,9],[3,0,8]]needs = [3,2]
- Output:
- 22
- Explanation:
Buy the bundle of three first-kind items for 8, then two second-kind items for 7 each: 8 + 14 = 22. Buying everything one by one costs 12 + 14 = 26.
Example 2
- Input:
- price = [5,3,6]specials = [[2,0,1,10],[1,1,0,6],[0,0,0,1]]needs = [3,2,1]
- Output:
- 19
- Explanation:
Use the bundle (2,0,1) for 10 and the bundle (1,1,0) for 6, then one more second-kind item for 3: 19. The last bundle is empty and is ignored.
Example 3
- Input:
- price = [10], specials = [[2,15]], needs = [3]
- Output:
- 25
- Explanation:
One bundle of two items costs 15, and the third item is bought alone for 10: 25. Two bundles would give four items, more than needed, which is not allowed.
Constraints
- 1 ≤
n=price.length≤ 5 - 0 ≤
needs[i]≤ 4 - 0 ≤
specials.length≤ 30; every row hasn + 1numbers, the last one being the bundle price - 1 ≤
price[i]≤ 10; 0 ≤ bundle contents and prices ≤ 50
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 800 msC++ 200 msJava 400 msJavaScript 400 msTypeScript 400 ms
Expected complexity
- Time
- O(S * 5^n)
- Space
- O(5^n)