题目描述
在LeetCode商店中, 有许多在售的物品。
然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。
现给定每个物品的价格,每个大礼包包含物品的清单,以及待购物品清单。请输出确切完成待购清单的最低花费。
每个大礼包的由一个数组中的一组数据描述,最后一个数字代表大礼包的价格,其他数字分别表示内含的其他种类物品的数量。
任意大礼包可无限次购买。
你不可以购买超出待购清单的物品,即使更便宜。
示例:
输入: [2,5], [[3,0,5],[1,2,10]], [3,2]
输出: 14
解释:
有A和B两种物品,价格分别为¥2和¥5。
大礼包1,你可以以¥5的价格购买3A和0B。
大礼包2, 你可以以¥10的价格购买1A和2B。
你需要购买3个A和2个B, 所以你付了¥10购买了1A和2B(大礼包2),以及¥4购买2A。
Sample Code 1
public class Solution {
Map<List<Integer>, Integer> memo = new HashMap();
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
if (memo.containsKey(needs))
return memo.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) + shoppingOffers(price, special, clone));
}
memo.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;
}
}
Sample Code 2
最快(?)
class Solution {
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
return shoppingOffers(price,special, needs, 0);
}
private int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs, int pos){
int local_min = directParchase(price, needs);
for(int i = pos; i < special.size() ; i ++){
List<Integer> tmp = new ArrayList<>();
List<Integer> offer = special.get(i);
for(int j = 0 ; j < needs.size(); j ++){
if(offer.get(j) > needs.get(j)){
tmp = null;
break;
}
tmp.add(needs.get(j) - offer.get(j));
}
if(tmp != null)
local_min = Math.min(local_min, offer.get(offer.size() - 1) + shoppingOffers(price, special, tmp, i));
}
return local_min;
}
private int directParchase(List<Integer> price, List<Integer> needs){
int sum = 0;
for(int i = 0; i < needs.size(); i++)
sum += price.get(i) * needs.get(i);
return sum;
}
}
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shopping-offers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。