ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【力扣】638. 大礼包

2021-10-24 12:04:45  阅读:249  来源: 互联网

标签:get 638 price List 力扣 int 大礼包 filterSpecial


638. 大礼包

在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有对应的价格。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。
给你一个整数数组 price 表示物品价格,其中 price[i] 是第 i 件物品的价格。另有一个整数数组 needs 表示购物清单,其中 needs[i] 是需要购买第 i 件物品的数量。
还有一个数组 special 表示大礼包,special[i] 的长度为 n + 1 ,其中 special[i][j] 表示第 i 个大礼包中内含第 j 件物品的数量,且 special[i][n] (也就是数组中的最后一个整数)为第 i 个大礼包的价格。
返回 确切 满足购物清单所需花费的最低价格,你可以充分利用大礼包的优惠活动。你不能购买超出购物清单指定数量的物品,即使那样会降低整体价格。任意大礼包可无限次购买。
示例 1:

输入:price = [2,5], special = [[3,0,5],[1,2,10]], needs = [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:

输入:price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [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 更加便宜。

提示:

  • n == price.length
  • n == needs.length
  • 1 <= n <= 6
  • 0 <= price[i] <= 10
  • 0 <= needs[i] <= 10
  • 1 <= special.length <= 100
  • special[i].length == n + 1
  • 0 <= special[i][j] <= 50

题解

记忆化搜索+dfs

class Solution {
    Map<List<Integer>, Integer> memo = new HashMap<>();
    public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
        int n = price.size();

        // 过滤不需要的大礼包,只保留需要计算的大礼包
        List<List<Integer>> filterSpecial = new ArrayList<List<Integer>>();
        for(List<Integer> sp : special){
            int totalCount = 0, totalPrice = 0;
            for(int i = 0; i < n; i++){
                totalCount += sp.get(i);
                totalPrice += sp.get(i) * price.get(i);
            }
            if(totalCount > 0 && totalPrice > sp.get(n)){   // 该大礼包更便宜,可以添加
                filterSpecial.add(sp);
            }
        }

        return dfs(price, filterSpecial, needs, n);
    }

    // 记忆化搜索满足购物清单所需花费的最低价格
    public int dfs(List<Integer> price, List<List<Integer>> filterSpecial, List<Integer> curNeeds, int n) {
        if(!memo.containsKey(curNeeds)){
            int minPrice = 0;
            for(int i = 0; i < n; i++){
                minPrice += curNeeds.get(i) * price.get(i);   // 不买大礼包,原价购买清单中所有物品的花费
            }
            for(List<Integer> curSpecial : filterSpecial){   // 遍历所有大礼包得到的情况
                int specialPrice = curSpecial.get(n);
                List<Integer> nextNeeds = new ArrayList<>();
                for(int i = 0; i < n; i++){
                    if(curSpecial.get(i) > curNeeds.get(i)){  // 每个商品不能超过购买清单的指定数量
                        break;
                    }
                    nextNeeds.add(curNeeds.get(i) - curSpecial.get(i));
                }
                if(nextNeeds.size() == n){  // 可以购买该大礼包
                    minPrice = Math.min(minPrice, dfs(price, filterSpecial, nextNeeds, n) + specialPrice);  // dfs
                }
            }
            memo.put(curNeeds, minPrice);  // 将当前需要清单对应的最低价格加入记忆
        }
        return memo.get(curNeeds);
    }
}

不使用记忆化搜索也行

class Solution {
    Map<List<Integer>, Integer> memo = new HashMap<>();
    public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
        int n = price.size();

        // 过滤不需要的大礼包,只保留需要计算的大礼包
        List<List<Integer>> filterSpecial = new ArrayList<List<Integer>>();
        for(List<Integer> sp : special){
            int totalCount = 0, totalPrice = 0;
            for(int i = 0; i < n; i++){
                totalCount += sp.get(i);
                totalPrice += sp.get(i) * price.get(i);
            }
            if(totalCount > 0 && totalPrice > sp.get(n)){   // 该大礼包更便宜,可以添加
                filterSpecial.add(sp);
            }
        }

        return dfs(price, filterSpecial, needs, n);
    }

    // 记忆化搜索满足购物清单所需花费的最低价格
    public int dfs(List<Integer> price, List<List<Integer>> filterSpecial, List<Integer> curNeeds, int n) {
        int minPrice = 0;
        for(int i = 0; i < n; i++){
            minPrice += curNeeds.get(i) * price.get(i);   // 不买大礼包,原价购买清单中所有物品的花费
        }
        for(List<Integer> curSpecial : filterSpecial){   // 遍历所有大礼包得到的情况
            int specialPrice = curSpecial.get(n);
            List<Integer> nextNeeds = new ArrayList<>();
            for(int i = 0; i < n; i++){
                if(curSpecial.get(i) > curNeeds.get(i)){  // 每个商品不能超过购买清单的指定数量
                    break;
                }
                nextNeeds.add(curNeeds.get(i) - curSpecial.get(i));
            }
            if(nextNeeds.size() == n){  // 可以购买该大礼包
                minPrice = Math.min(minPrice, dfs(price, filterSpecial, nextNeeds, n) + specialPrice);  // dfs
            }
        }
        return minPrice;
    }
}

标签:get,638,price,List,力扣,int,大礼包,filterSpecial
来源: https://blog.csdn.net/weixin_41317766/article/details/120931964

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有