In LeetCode shop, there are many items in the sale.
However, there are some spree, each spree for a price bundling a group of articles.
Given the current list price of each item, each gift package contains items, as well as a list of your purchases to be. Please complete the minimum output of the exact cost of the wish list.
Each package is a large array is described by a set of data, the last number represents the price of a large package, the number of other types of other digital items are contained FIG.
Any spree can buy unlimited.
thought:分为3步:
1. 每种物品都单独购买,需要money1
2. 用大礼包进行替换, 需要money2
3. 取最小值min(money1, money2)
减枝:
1. 当礼包中的物品的数量 > 所需物品的数量, 要进行减枝
2. 为了避免像,礼包1,2 和 礼包2, 1这种情况重复计算两次,可以使用pos来指向当前的位置
允许获取的礼包的索引只能大于等于pos(这种减枝比较隐蔽)
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;
}