638. Shopping Offers [LeetCode]
来源:互联网 发布:无法连接steam网络 编辑:程序博客网 时间:2024/06/07 02:31
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:
Input: [2,5], [[3,0,5],[1,2,10]], [3,2]Output: 14Explanation: There are two kinds of items, A and B. Their prices are $2 and $5 respectively. In special offer 1, you can pay $5 for 3A and 0BIn special offer 2, you can pay $10 for 1A and 2B. You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
Example 2:
Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]Output: 11Explanation: The price of A is $2, and $3 for B, $4 for C. You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. You cannot add more items, though only $9 for 2A ,2B and 1C.
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.
for j = 1 to needs.size():
public class Solution { public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) { int result = Integer.MAX_VALUE; // dfs, try apply each offer to the needs, and then add back for (int i=0; i<special.size(); i++) { // get the target offer List<Integer> offer = special.get(i); // set offer application default valid boolean valid = true; // try to apply offer for (int j=0; j<needs.size(); j++) { // the remain need count after apply the offer int remain = needs.get(j) - offer.get(j); // set needs with new value needs.set(j, remain); if (valid && remain<0) // offer count is more than need, quit this offer valid = false; } if (valid) // recurse calculate the remaining needs result = Math.min(result, shoppingOffers(price, special, needs)+offer.get(needs.size())); for (int j=0; j<needs.size(); j++) { // add back to needs int origin = needs.get(j)+offer.get(j); needs.set(j, origin); } } // then calculate price with non offer, and choose the smaller one return int nonOffer = 0; for (int i=0; i<needs.size(); i++) { nonOffer += price.get(i) * needs.get(i); } return Math.min(result, nonOffer); }}
- 638. Shopping Offers [LeetCode]
- [leetcode]638. Shopping Offers
- [LeetCode]638. Shopping Offers
- leetcode 638. Shopping Offers
- [leetcode] 638. Shopping Offers
- 638. Shopping Offers
- 638. Shopping Offers
- 638.Shopping Offers
- 638. Shopping Offers
- 638. Shopping Offers
- leetcode 638. Shopping Offers 递归求解最佳购物优惠
- (M)DFS: 638. Shopping Offers
- LeetCode算法学习日志-638 Shopping Offers
- Shopping Offers
- Shopping Offers
- Shopping Offers
- Shopping Offers
- USACO 3.3 Shopping Offers (shopping)
- 对List集合中的元素进行排序
- memcached--存储命令--add
- 环境变量
- 一个STM8S ADC脚与其它功能复用时的话题
- BFC的理解
- 638. Shopping Offers [LeetCode]
- 在IDEA中使用junit
- Linux Kernel ROP
- 编码
- 2017.7.18-学习日志-大数据
- g721解码使用ffmpeg和g726一样
- RHC-124--第二节
- CentOS7.2部署FTP
- HDU 5821 Ball(贪心)