[leetcode] 638. Shopping Offers

Question :

In LeetCode Store, there are some kinds of items to sell. Each item has a price.

However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.

You are given the each item’s price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers.

Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.

You could use any of special offers as many times as you want.

Example 1:
Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
Output: 14
Explanation: 
There are two kinds of items, A and B. Their prices are $2 and $5 respectively. 
In special offer 1, you can pay $5 for 3A and 0B
In special offer 2, you can pay $10 for 1A and 2B. 
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
Example 2:
Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
Output: 11
Explanation: 
The price of A is $2, and $3 for B, $4 for C. 
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. 
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. 
You cannot add more items, though only $9 for 2A ,2B and 1C.

Note:

  1. There are at most 6 kinds of items, 100 special offers.
  2. For each item, you need to buy at most 6 of them.
  3. You are not allowed to buy more items than you want, even if that would lower the overall price.

Solution 1:

随便找了一道DFS的题来做,因此看到题目后首先想到的就是用递归遍历全部的可能性,找出最小的价格。
一开始想的是,以每个状态为节点,递归遍历所有的节点,对于每一个节点,pricespecial offers是不变的,needs会根据上一个状态使用的offer 的变化而变化。对于每一个节点,尝试使用每一个offer 并往下递归再和不使用offer 来比较,选出最小的价格返回。
但是这样做会有一定的重复性,比如下图

   开始                开始
   /                    \
  1                      2
   \                    /
    2                  1

用数字表示使用第几个offer,树的层数表示遍历的层数,那么上面两棵树遍历到第二层的时候是完全一样的,都是使用了offer1offer2 这样就会有重复遍历的情况。
因此,就修改了一下思路,对于第一次遍历以后的遍历,可以使用的offer,只能是当前使用的offeri的后续offerk(k >= i)。简单来说,就是在第一次递归的时候就划分为n种情况(n = special.size()),分别是:使用n种offer的情况,不使用offer1的情况,不使用offer2的情况······
这个优化对应于代码中的kk == -1时表示第一次递归。
这样做能有效减少很多种重复的情况。但是其实还是会有重复的情况。对于不同的节点,不同的地方就在于needs的不同,因此,递归树种肯定存在相同的节点,就是通过不同special的组合,产生了相同的新的needs的情况,所以其实可以用map保存不同needs对应的最小价格,如果要求相同的needs的情况时,就可以直接查找map表,而不用递归下去。因此该方法会在Solution 2给出,如果我还记得写个Solution 2的话。

class Solution {
public:
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
        return helper(price, special, needs, -1);
    }

    int helper(vector<int>& price, vector<vector<int>>& special, vector<int>& needs, int k) {
        int ret = 0;
        for (int i = 0; i < price.size(); i++) {
            ret += price[i] * needs[i];
        }

        for (int i = ((k == -1) ? 0 : k); i < special.size(); i++) {
            bool flag = true;
            for (int j = 0; j < needs.size(); j++) {
                if (needs[j] - special[i][j] < 0) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                vector<int> newNeeds;
                for (int j = 0; j < needs.size(); j++) {
                    newNeeds.push_back(needs[j] - special[i][j]);
                }

                int temp = special[i][needs.size()] + helper(price, special, newNeeds, ((k == -1) ? i : k));
                ret = min(ret, temp);
            }
        }
        return ret;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值