leetcode 638. Shopping Offers
来源:互联网 发布:淘宝促销活动海报 编辑:程序博客网 时间:2024/06/05 07:17
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.
这道题就是寻常的递归with memory就能解决。
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {HashMap<List<Integer>, Integer> map=new HashMap<List<Integer>, Integer>();return helper(price, special, needs, needs.size(), map);}public int helper(List<Integer> price, List<List<Integer>> special, List<Integer> needs,int n,HashMap<List<Integer>, Integer> map){if(map.get(needs)!=null){return map.get(needs);}int minPrice=0;//下面是完全不用套餐的情况for(int i=0;i<n;i++){minPrice+=(needs.get(i)*price.get(i));}//下面是用套餐的情况for(List<Integer> taocan:special){boolean canUseThisTaocan=true;int thisPrice=0;for(int i=0;i<n;i++){if(needs.get(i)<taocan.get(i)){//套餐里的内容超过我需要的数目了canUseThisTaocan=false;break;}}if(canUseThisTaocan){List<Integer> needClone=new ArrayList<>(needs);for(int i=0;i<n;i++){needClone.set(i, needClone.get(i)-taocan.get(i));}thisPrice=taocan.get(n)+helper(price, special, needClone, n, map);if(thisPrice<minPrice){minPrice=thisPrice;}}}map.put(needs, minPrice);return minPrice;}这里就要说的是list作为map的key,hashcode方法被定义在这里。https://docs.oracle.com/javase/8/docs/api/java/util/List.html#hashCode-- 。
hashCode
int hashCode()
int hashCode = 1; for (E e : list) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
这道题有solutions: https://leetcode.com/problems/shopping-offers/solution/ 解法跟我的一样,就是递归with memory.
Solution
Approach #1 Using Recursion [Accepted]
Algorithm
Before discussing the steps involved in the process, we need to note a few points. Firstly, whenever an offer is used from amongst the ones available in the special list, we need to update the needs appropriately, such that the number of items in the current offer of each type are deducted from the ones in the corresponding entry in needs.
Further, an offer can be used only if the number of items, of each type, required for using the offer, is lesser than or equal to the ones available in the current needs.
Now, let's discuss the algorithm. We make use of a shopping(price,special,needs)
function, which takes the price and special list along with the current(updated) needs as the input and returns the minimum cost of buying these items as required by this needs list.
In every call of the function shopping(price,special,needs)
, we do as follows:
Determine the cost of buying items as per the needs array, without applying any offer. Store the result in res.
Iterate over every offer in the special list. For every offer chosen, repeat steps 3 to 5.
Create a copy of the current needs in a clone list(so that the original needs can be used again, while selecting the next offer).
Try to apply the current offer. If possible, update the required number of items in clone.
If the current offer could be applied, find the minimum cost out of res and
offer\current +shopping(price,special,clone)
. Here,offer\current refers to the price that needs to be paid for the current offer. Update the res with the minimum value.Return the res corresponding to the minimum cost.
We need to note that the clone needs to be updated afresh from needs(coming to the current function call) when we choose a new offer. This needs to be done, because solely applying the next offer could result in a lesser cost than the one resulting by using the previous offer first.
Java
public class Solution { public int shoppingOffers(List < Integer > price, List < List < Integer >> special, List < Integer > needs) { return shopping(price, special, needs); } public int shopping(List < Integer > price, List < List < Integer >> special, List < Integer > needs) { int j = 0, res = dot(needs, price); for (List < Integer > s: special) { ArrayList < Integer > clone = new ArrayList < > (needs); for (j = 0; j < needs.size(); j++) { int diff = clone.get(j) - s.get(j); if (diff < 0) break; clone.set(j, diff); } if (j == needs.size()) res = Math.min(res, s.get(j) + shopping(price, special, clone)); } return res; } public int dot(List < Integer > a, List < Integer > b) { int sum = 0; for (int i = 0; i < a.size(); i++) { sum += a.get(i) * b.get(i); } return sum; }}
Approach #2 Using Recursion with memoization [Accepted]
Algorithm
In the last approach, we can observe that the same needs can be reached by applying the offers in various orders. e.g. We can choose the first offer followed by the second offer or vice-versa. But, both lead to the same requirement of updated needs and the cost as well. Thus, instead of repeating the whole process for the same needs state through various recursive paths, we can create an entry corresponding to the current set of needs in a HashMap, map, which stores the minimum cost corresponding to this set of needs. Thus, whenever the same call is made again in the future through a different path, we need not repeat the whole process over, and we can directly return the result stored in the map.
Java
public class Solution { public int shoppingOffers(List < Integer > price, List < List < Integer >> special, List < Integer > needs) { Map < List < Integer > , Integer > map = new HashMap(); return shopping(price, special, needs, map); } public int shopping(List < Integer > price, List < List < Integer >> special, List < Integer > needs, Map < List < Integer > , Integer > map) { if (map.containsKey(needs)) return map.get(needs); int j = 0, res = dot(needs, price); for (List < Integer > s: special) { ArrayList < Integer > clone = new ArrayList < > (needs); for (j = 0; j < needs.size(); j++) { int diff = clone.get(j) - s.get(j); if (diff < 0) break; clone.set(j, diff); } if (j == needs.size()) res = Math.min(res, s.get(j) + shopping(price, special, clone, map)); } map.put(needs, res); return res; } public int dot(List < Integer > a, List < Integer > b) { int sum = 0; for (int i = 0; i < a.size(); i++) { sum += a.get(i) * b.get(i); } return sum; }}
- 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)
- POJ-2823--Sliding Window--双端队列实现单调队列
- Mysql字符集设置指南_MySQL教程
- OAI搭建之ENB
- redis 多个项目之间消息队列传递
- rabbitmq 管理 及 常用命令
- leetcode 638. Shopping Offers
- 2017 Multi-University Training Contest
- 安卓开发 Jni新手初步尝试
- Bad Request: "requirement failed: Local path /root/.livy-sessions/61a5e39c-d199-4bb8-967b-960b4e3e9
- 保护 iOS 用户数据安全: Keychain 和 Touch ID
- 设置Mysql5.6允许外网访问详细流程
- hdfs安全模式
- 擅长排列的小明
- 设计师必须了解的找工作秘籍