LeetCode 638. 大礼包(记忆化搜索,dfs)

编程入门 行业动态 更新时间:2024-10-20 11:51:45

LeetCode 638. <a href=https://www.elefans.com/category/jswz/34/1762751.html style=大礼包(记忆化搜索,dfs)"/>

LeetCode 638. 大礼包(记忆化搜索,dfs)

  1. 大礼包
    在 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 更加便宜。

看题解的,但是完全不用设置map,因为最后得到的minprice就是最小的值了。
总的思路是:先对大礼包special进行处理,如果礼包总价比单买还贵,则该礼包不可能被购买,直接过滤掉,得到过滤后的礼包filterSpecial,并将其传入dfs中.
重点:对于dfs购买的话,有两种选择。
每次dfs。
选择一:不买礼包,直接按照单价购买。则价格minprice1=对应单价*对应数量。

选择二:购买礼包,价格=礼包的价格。但是购买礼包的话,可能不够数量。则需要将新的curneeds继续进行dfs,最终得到minprice2.
购买当前数量的物品的最小价格为:minprice=Math.min(minprice1,minprice2)

class Solution {// Map<List<Integer>, Integer> memo = new HashMap<List<Integer>, Integer>();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;int totalPrice = 0;for (int i = 0; i < n; ++i) {//totalCount += sp.get(i);totalPrice += sp.get(i) * price.get(i);}if ( totalPrice > sp.get(n)) {filterSpecial.add(sp);}}return dfs(price, special, needs, filterSpecial, n);}// 记忆化搜索计算满足购物清单所需花费的最低价格public int dfs(List<Integer> price, List<List<Integer>> special, List<Integer> curNeeds, List<List<Integer>> filterSpecial, 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> nxtNeeds = new ArrayList<Integer>();for (int i = 0; i < n; ++i) {if (curSpecial.get(i) > curNeeds.get(i)) { // 不能购买超出购物清单指定数量的物品break;}nxtNeeds.add(curNeeds.get(i) - curSpecial.get(i));}if (nxtNeeds.size() == n) { // 大礼包可以购买minPrice = Math.min(minPrice, dfs(price, special, nxtNeeds, filterSpecial, n) + specialPrice);}}return minPrice;// memo.put(curNeeds, minPrice);// }// return memo.get(curNeeds);}
}

更多推荐

LeetCode 638. 大礼包(记忆化搜索,dfs)

本文发布于:2024-02-24 15:26:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1695764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大礼包   记忆   LeetCode   dfs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!