前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >638. 大礼包 Krains 2020-08-01 19:48:29 动态规划DFS

638. 大礼包 Krains 2020-08-01 19:48:29 动态规划DFS

作者头像
Krains
发布2020-08-05 11:50:13
3160
发布2020-08-05 11:50:13
举报
文章被收录于专栏:Krains

# 题目链接

# DFS加记忆化

解题思路

  • 将原价物品打包成大礼包,统一进行处理
  • 如果最优解中包含一种大礼包,那么该大礼包一定是买到不能买为止的。
  • 使用DFS搜索所有可能购买的方案,对于相同的needs来说最优解是一定的,所以可以加记忆化。
代码语言:javascript
复制
class Solution {
    Map<List<Integer>, Integer> memo = new HashMap<>();

    public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
        // 将原价物品也打包成大礼包,方便处理
        for(int i = 0; i < price.size(); i++){
            List<Integer> list = new ArrayList<>();
            for(int j = 0; j < price.size(); j++){
                if(j != i) 
                    list.add(0);
                else
                    list.add(1);
            }
            list.add(price.get(i));
            special.add(list);
        }

        // 将needs全为0状态放入
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < needs.size(); i++)
            list.add(0);
        memo.put(list, 0);

        return helper(special, needs);
    }

    public int helper(List<List<Integer>> special, List<Integer> needs) {
        if(memo.containsKey(needs))
            return memo.get(needs);

        int i;
        int cost = (int)1e8;
        for(List<Integer> list : special){
            // k为最多购买k件当前大礼包
            int k = (int)1e8;
            for(i = 0; i < list.size()-1; i++){
                if(list.get(i) != 0){
                    k = Math.min(k, needs.get(i) / list.get(i));
                }
            }
            // 如果当前礼包无法购买,考虑下一个礼包
            if(k == 0)
                continue;

            // 购买k件当前大礼包
            List<Integer> temp = new ArrayList<>();
            for(i = 0; i < list.size() - 1; i++)
                temp.add(needs.get(i) - k * list.get(i));
            cost = Math.min(cost, helper(special, temp) + k * list.get(i));
        }
        
        // 记忆
        memo.put(needs, cost);
        return cost;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # 题目链接
  • # DFS加记忆化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档