力扣: 638. 大礼包

编程入门 行业动态 更新时间:2024-10-20 15:56:14

力扣: 638. <a href=https://www.elefans.com/category/jswz/34/1762751.html style=大礼包"/>

力扣: 638. 大礼包

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<>();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, special, needs, filterSpecial, n);}private int dfs(List<Integer> price, List<List<Integer>> special, List<Integer> needs, List<List<Integer>> filterSpecial, int n ){if (!memo.containsKey(needs)){int minPrice = 0;for (int i = 0; i < n; i++){minPrice += needs.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) > needs.get(i) ){break;}nextNeeds.add(needs.get(i) - curSpecial.get(i));}if (nextNeeds.size() == n){minPrice = Math.min(minPrice, dfs(price, special, nextNeeds, filterSpecial, n) + specialPrice);}}memo.put(needs, minPrice);}return memo.get(needs);}
}

更多推荐

力扣: 638. 大礼包

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

发布评论

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

>www.elefans.com

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