LeetCode算法学习日志-638 Shopping Offers

LeetCode 638 Shopping Offers

题目原文

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 forexactly 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.

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.

题意分析

由于题目要求必须刚好满足需求,不能买少也不能买多,所以对于某种需求,如果有一个数量小于所有special offer相应产品的数量,则不能使用任何special offer,相应花销为零售价。

解法分析

本题是一个最优化问题,当needs的任何一项小于某个special offer的相应一项时,则不能使用此offer,对每一个special offer均有这种情况时,只能按照单价购买。此时需要注意,使用special offer不一定比不使用好,所以在存储相应needs所对应的最低价时,初始值要设置为单价购买时的价格,本题我才用两种动态规划方法解:

  • 自底向上的动态规划方法
  • 自顶向下带存储的动态规划方法

自底向上

采用自底向上方法首先要找到最优子结构,并且一般需要构造多维数组来存储中间值,通过遍历这个数组来逐步得到最终结果。先用单价求出数组初始值,needs大于special offer时,遍历不同special offer,得到最小价格。

class Solution {
public:
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs)
{
	int n = price.size();
	for (int i = n; i < 6; i++)
	{
		price.push_back(0);
		needs.push_back(0);
	}
	for (int i = special.size() - 1; i >= 0; i--)  // fill special to 6 items
	{
		int t = special[i][n];
		special[i][n] = 0;
		for (int j = n + 1; j < 7; j++)
			special[i].push_back(0);
		special[i][6] = t;
	}
	int dp[7][7][7][7][7][7], m = special.size();
	for (int j = 0; j <=needs[0]; j++)
	{
		for (int k = 0; k <=needs[1]; k++)//要遍历,但长度不能确定,所以事先确定长度
		for (int p = 0; p <=needs[2]; p++)
		for (int q = 0; q <=needs[3]; q++)
		for (int r = 0; r <=needs[4]; r++)
		for (int s = 0; s <=needs[5]; s++)
			dp[j][k][p][q][r][s]=j*price[0]+k*price[1]+p*price[2]+q*price[3]+r*price[4]+s*price[5];
	}
	for (int i = 0; i < m; i++)  // then it just a dynamic programming problem
	{
		for (int j = special[i][0]; j <=needs[0]; j++)
		for (int k = special[i][1]; k <=needs[1]; k++)
		for (int p = special[i][2]; p <=needs[2]; p++)
		for (int q = special[i][3]; q <=needs[3]; q++)
		for (int r = special[i][4]; r <=needs[4]; r++)
		for (int s = special[i][5]; s <=needs[5]; s++)
		{
			int tt=dp[j-special[i][0]][k-special[i][1]][p-special[i][2]]
				[q-special[i][3]][r-special[i][4]][s-special[i][5]];
			if (tt != INT_MAX)
				dp[j][k][p][q][r][s]=min(dp[j][k][p][q][r][s],tt+special[i][6]);
		}
	}
	return dp[needs[0]][needs[1]][needs[2]][needs[3]][needs[4]][needs[5]];
}
};

由于needs的长度是一个动态量,而采用自底向上的方法需要遍历needs的每一个元素,为了确定循环变量j,k,p,q,r,s,我们将needs补为6位确定大小。由于special offer不一定划算,所以先将dp存储为单价买入时的花费,之后再通过比大小确定最终值。

自顶向下

自顶向下可以用needs求出相应的key,作为存储买入值容器的key。首先还是要利用单价求出不同needs对应的买入价初始值,然后遍历不同special offer,需要定义一个temp用于存储减去special offer后的新needs,因为needs的值不能被改变,要判断needs是否比special offer大,只需要在遍历时判断相减结果是否大于零,如果小于则退出循环。c++代码如下:

class Solution {
private:
    unordered_map <int,int> myMap;
public:
    int getKey(vector<int> needs){
        int n=needs.size();
        int i;
        int key=0;
        for(i=n-1;i>=0;i--){
            key+=needs[i]*(int)pow(double(10),(n-1-i));            
        }
        return key;
    }
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
        int n=price.size();
        int m=special.size();
        int minPay=0;
        vector<int> temp;
        int key;
        key=getKey(needs);
        if(myMap[key])
            return myMap[key];
        int i,j;
        for(i=0;i<n;i++){
            minPay=price[i]*needs[i]+minPay;
        }
        for(i=0;i<m;i++){
            temp=needs;
            for(j=0;j<n;j++){
                temp[j]=needs[j]-special[i][j];
                if(temp[j]<0)
                    break;
            }
            if(j==n)
                minPay=min(minPay,special[i][n]+shoppingOffers(price,special,temp));  
        }
        myMap[key]=minPay;
        return minPay;  
    }
};
此题采用自顶向下的方法也得到了不错结果,对于表征needs比较困难的情况(维数比较高),可以将其转化为十进制数作为key,用自顶向下的方法进行计算。
             
   



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值