In LeetCode Store, there are some kinds of items to sell. Each item has a price.
However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
You are given the each item’s price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers.
Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.
You could use any of special offers as many times as you want.
Example 1:
Example 2:
Note:
There are at most 6 kinds of items, 100 special offers.
For each item, you need to buy at most 6 of them.
You are not allowed to buy more items than you want, even if that would lower the overall price.
解法1:
用了一个DFS + memo的做法,基本思路是去尝试每一个deal,当needs全都满足的时候更新一下最小的cost。
要注意的是用一个memo table去记录已经探寻过的一种needs对应的cost。
同时对每一个needs要考虑是否不用任何一个deal,就是说对于每个商品单独相加花费更少。
C++
Java