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:
1 | Input: [2,5], [[3,0,5],[1,2,10]], [3,2] |
Example 2:
1 | Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1] |
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.
解题思路
这道题的题目意思是,给了我们商品单价,和一些优惠券(优惠券的格式是前N个数字代表第N个商品有几个,最后一个数字是总价钱),就是说给了我们单价和打包价格,然后还给了我们需要买的每个商品的个数。让我们找到一个最优解,即买到需要的商品花费的最少钱数。
这里我们首先以不使用优惠券所花费的钱数作为初始值,然后对所有优惠券做一个递归,最后更新结果(res)的值就可以了。看代码吧
解题代码【.CPP】
1 | class Solution { |