leetcode_638_超市大礼包购买

  1. 大礼包
    在LeetCode商店中, 有许多在售的物品。

然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。

现给定每个物品的价格,每个大礼包包含物品的清单,以及待购物品清单。请输出确切完成待购清单的最低花费。

每个大礼包的由一个数组中的一组数据描述,最后一个数字代表大礼包的价格,其他数字分别表示内含的其他种类物品的数量。

任意大礼包可无限次购买。

示例 1:

输入: [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。
示例 2:

输入: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
输出: 11
解释:
A,B,C的价格分别为¥2,¥3,¥4.
你可以用¥4购买1A和1B,也可以用¥9购买2A,2B和1C。
你需要买1A,2B和1C,所以你付了¥4买了1A和1B(大礼包1),以及¥3购买1B, ¥4购买1C。
你不可以购买超出待购清单的物品,尽管购买大礼包2更加便宜。
说明:

最多6种物品, 100种大礼包。
每种物品,你最多只需要购买6个。
你不可以购买超出待购清单的物品,即使更便宜。
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
思路就是:
在大礼包数组当中一个一个去比较是否能用。因为不允许超买现象。看代码注释

class Solution {
    private int sum_price;

    /**
     * 当前给定价格和对应的需求,直接购买需要的钱数
     * @param price:单价价格
     * @param needs:需求量
     * @return 直接购买的钱数
     */
    public int directBuy(List<Integer> price, List<Integer> needs){
        int total = 0;
        for (int i = 0; i < needs.size(); i++) {
            total += price.get(i) * needs.get(i);
        }
        return total;
    }

    public boolean canUse(List<Integer> offers, List<Integer> needs){
        boolean temp = true;
        for (int i = 0; i < needs.size(); i++) {
            if (offers.get(i) > needs.get(i)) {
                temp = false;
                break;
            }
        }
        return temp;
    }


    /**
     *
     * @param price:商品单价数组
     * @param needs:商品需求数组
     * @param specials:大礼包集合数组
     * @param index:大礼包当前使用到的下标
     * @param cur_used:当前的花销
     */
    public void helper(List<Integer> price, List<Integer> needs, List<List<Integer>> specials, int index, int cur_used){
        if (cur_used > this.sum_price) return;
        if (index == specials.size()) {
            cur_used += directBuy(price, needs);
            if (cur_used < this.sum_price) this.sum_price = cur_used;
            return;
        }

        List<Integer> offer = specials.get(index);  //  大礼包当中的优惠策略可以使用
        if (this.canUse(offer, needs)) {
            List<Integer> newNeeds = new ArrayList<>();
            for (int i = 0; i < needs.size(); i++) {
//                needs.set(i, needs.get(i) - offer.get(i)) ;
                newNeeds.add(needs.get(i) - offer.get(i));
            }

            helper(price, newNeeds, specials, index, cur_used + offer.get(offer.size()-1));
        }
        helper(price, needs, specials, index+1, cur_used);  // 大礼包当中的优惠策略不可以使用
    }

    /**
     *
     * @param price:单品价格
     * @param special:大礼包
     * @param needs:需求数组
     * @return 返回需要用到的钱数
     */
    public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
        this.sum_price = directBuy(price, needs);
        helper(price, needs, special, 0, 0);
        return this.sum_price;
    }
    
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值