[leetcode] 638. Shopping Offers

xiaoxiao2021-02-28  16

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:

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

Solution 1:

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

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

用数字表示使用第几个offer,树的层数表示遍历的层数,那么上面两棵树遍历到第二层的时候是完全一样的,都是使用了offer1和offer2 这样就会有重复遍历的情况。 因此,就修改了一下思路,对于第一次遍历以后的遍历,可以使用的offer,只能是当前使用的offeri的后续offerk(k >= i)。简单来说,就是在第一次递归的时候就划分为n种情况(n = special.size()),分别是:使用n种offer的情况,不使用offer1的情况,不使用offer2的情况······ 这个优化对应于代码中的k,k == -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; } };
转载请注明原文地址: https://www.6miu.com/read-1099993.html

最新回复(0)