https://leetcode.com/problems/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 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.

1.最多6种物品,每种最多买6个。所以一开始想到的是,用一个整数的每一位来表示一种物品买了多少个,dp[666666]表示6种物品每种买6个。这样可以将问题转化为一个完全背包问题。

struct Item
{
int val,cost;
Item(int v,int c):val(v),cost(c) {}
bool operator < (const Item& item)const
{
return item.cost>cost;
}
};
class Solution
{
public:
bool legal(int x)
{
while(x>)
{
if(x%>)
return ;
x/=;
}
return ;
}
bool bigger(int a, int b)
{
while(a>&&b>)
{
if(a% < b%)
return ;
a/=;
b/=;
}
if(a>)
return ;
else if(a== && b==)
return ;
else
return ;
}
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs)
{
int all=;
for(int i=,w=; i<needs.size(); i++,w*=)
all += needs[i]*w;
vector<int> dp(all+,INT_MAX/);
dp[]=;
vector<Item> items;
for(int i=,w=; i<price.size(); i++,w*=)
items.push_back(Item(price[i], w));
for(int i=; i<special.size(); i++)
{
int cost=;
for(int j=,w=; j<special[i].size()-; j++,w*=)
cost += special[i][j]*w;
items.push_back(Item(special[i][special[i].size()-], cost));
}
for(int i=; i<items.size(); i++)
for(int j=items[i].cost; j<=all; j++)
if(legal(j) && bigger(j,j-items[i].cost))
{
dp[j] = min(dp[j], dp[j-items[i].cost] + items[i].val);
//cout<<dp[j]<<endl;
}
return dp[all];
}
};

DP

但是发现这样做的问题在于状态数最多有666666个,只用背包的解法复杂度很高,虽然过了,但是耗时2000ms,且使用70+Mb的内存空间。

2.第二种解法是用dfs,这里使用类似向量的运算来处理special,price和needs,重载运算符后很方便且可读性高。并且,dfs搜索的状态数远小于前一种解法。

bool operator < (const vector<int>& v, const int x)
{
for(auto& i:v)
if(i<x)
return true;
return false;
} bool operator <= (const vector<int>& v1, const vector<int>& v2)
{
for(int i=; i<v2.size(); i++)
if(v1[i]>v2[i])
return false;
return true;
} int operator * (const vector<int>& v1, const vector<int>& v2)
{
int ret=;
for(int i=; i<v1.size(); i++)
ret += v1[i] * v2[i];
return ret;
} vector<int> operator -= (vector<int>& v1, const vector<int>& v2)
{
for(int i=; i<v1.size(); i++)
v1[i] -= v2[i];
return v1;
} vector<int> operator += (vector<int>& v1, const vector<int>& v2)
{
for(int i=; i<v1.size(); i++)
v1[i] += v2[i];
return v1;
} class Solution
{
public:
int shoppingOffers(vector<int>& price, vector<vector<int> >& special, vector<int>& needs, int cost=)
{
if(needs < )
return INT_MAX;
int ret = cost + price * needs;
for(int i=; i<special.size(); i++)
{
if(special[i] <= needs)
{
needs -= special[i];
ret = min(ret, shoppingOffers(price, special, needs, cost+special[i].back()));
needs += special[i];
}
}
return ret;
}
};

dfs

这种解法耗时8ms,使用不到10Mb的空间。

最新文章

  1. 初识WEB:输入URL之后的故事
  2. 关于angularjs中ajax请求php接口参数个是转换的问题
  3. GitHub上最火的40个Android开源项目
  4. Interleaving Positive and Negative Numbers
  5. 一步步调试解决iOS内存泄漏
  6. 在美国看中国HTML5市场的发展
  7. NOIP200902分数线划定
  8. php 带cookie登陆
  9. 寻访上海西服定制店_Enjoy·雅趣频道_财新网
  10. Linux目录结构和常用命令
  11. ViewPagerIndicator的使用方法
  12. 常用的 JS 排序算法整理
  13. 服务端搭建——腾讯云通信(IM)
  14. Kali学习笔记32:Maltego、Exiftool
  15. 20145212罗天晨 WEB基础实践
  16. (23)ajax实现上传文件的功能
  17. 梳理源码:spring ioc容器加载的流程图
  18. java 集合 Se HashTreeSet
  19. ecplise自动提示失效,使用补全自动提示快捷键(Alt+/),但只显示“No Default Proposals”
  20. Android Bitmap Drawable byte[] InputStream 相互转换方法

热门文章

  1. js连等运算
  2. caioj1272&amp;&amp;codeforces 148D: 概率期望值3:抓老鼠
  3. python的for...in...if...语句
  4. MySql LOAD DATA 使用
  5. 【193】◀▶ PowerShell 官方资料索引
  6. 异常处理:Cannot attach the file as database &#39;membership&#39;.
  7. jdbc 分页
  8. ssi,服务器端包含,&lt;include file=&quot;&quot;&gt;
  9. springboot(四)拦截器和全局异常捕捉
  10. bind: Invalid argument