LeetCode 638 Shopping Offers
题目链接:
题解
dynamic programing
需要用到进制转换来表示状态,或者可以直接用一个vector来保存状态。
代码
1、未优化,超时代码:
class Solution {
public:
int max(int x,int y){ return x>y?x:y; }
int min(int x,int y){ return x>y?y:x; }
int dfs(vector<int>& price,vector<vector<int> >& special, map<vector<int>,int>& dp,vector<int> cur){
if(dp.count(cur)) return dp[cur];
int &res=dp[cur]=INF;
for(int i=0;i<special.size();i++){
bool flag=true;
vector<int> nex;
for(int j=0;j<price.size();j++){
int tmp=cur[j]-special[i][j];
if(tmp<0){
flag=false; break;
}
nex.push_back(tmp);
}
if(flag){
res=min(res,dfs(price,special,dp,nex)+special[i][special[i].size()-1]);
}
}
//这里可以优化,能优化的原因是因为转移顺序是可调整的
for(int i=0;i<price.size();i++){
vector<int> nex(cur);
if(nex[i]>0){
nex[i]--;
res=min(res,dfs(price,special,dp,nex)+price[i]);
}
}
return res;
}
int shoppingOffers(vector<int>& price, vector<vector<int> >& special, vector<int>& needs) {
map<vector<int>,int> dp;
vector<int> start;
for(int i=0;i<price.size();i++){
start.push_back(0);
}
dp[start]=0;
vector<int> cur(needs);
return dfs(price,special,dp,cur);
}
private:
const static int INF=0x3f3f3f3f;
};
2、优化之后的代码
class Solution {
public:
int max(int x,int y){ return x>y?x:y; }
int min(int x,int y){ return x>y?y:x; }
int dfs(vector<int>& price,vector<vector<int> >& special, map<vector<int>,int>& dp,vector<int> cur){
if(dp.count(cur)) return dp[cur];
int res=INF;
for(int i=0;i<special.size();i++){
bool flag=true;
vector<int> nex;
for(int j=0;j<price.size();j++){
int tmp=cur[j]-special[i][j];
if(tmp<0){
flag=false; break;
}
nex.push_back(tmp);
}
if(flag){
res=min(res,dfs(price,special,dp,nex)+special[i][special[i].size()-1]);
}
}
//这里进行了优化
int noSpecial=0;
for(int i=0;i<cur.size();i++){
noSpecial+=cur[i]*price[i];
}
res=min(res,noSpecial);
return dp[cur]=res;
}
int shoppingOffers(vector<int>& price, vector<vector<int> >& special, vector<int>& needs) {
map<vector<int>,int> dp;
vector<int> start;
for(int i=0;i<price.size();i++){
start.push_back(0);
}
dp[start]=0;
vector<int> cur(needs);
return dfs(price,special,dp,cur);
}
private:
const static int INF=0x3f3f3f3f;
};
LeetCode 638 Shopping Offers的更多相关文章
- Week 9 - 638.Shopping Offers - Medium
638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...
- LC 638. Shopping Offers
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- 【leetcode】638. Shopping Offers
题目如下: In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, ther ...
- 【LeetCode】638. Shopping Offers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯法 日期 题目地址:https://le ...
- 638. Shopping Offers
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)
Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...
- 洛谷P2732 商店购物 Shopping Offers
P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...
- poj 1170 Shopping Offers
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4696 Accepted: 1967 D ...
- USACO 3.3 Shopping Offers
Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the ...
随机推荐
- python第三十六课——2.迭代器对象
满足前提: 1).必须是一个可迭代对象 2).可以被next()所作用的 举例: generator... 高效的检测一个对象是否是迭代器对象 需要使用collections模块中的Iterator类 ...
- Odoo Model内容详解
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9390688.html 一:Odoo模型属性 1:_name 模型的唯一标识:如果没有继承其他模型 ...
- WorldWind源码剖析系列:下载队列类DownloadQueue
下载队列类DownloadQueue代表具有优先级的下载队列,该类的存储下载请求的数组链表专门按一定的优先级来存储下载请求的.该类的类图如下. 下载队列类DownloadQueue各个字段的含义说明如 ...
- Python2.7-os.path
os.path 模块,实现了对文件路径的操作,但是不操作文件.由于不同系统文件路径格式不同,os.path 总是调用适合当前系统的版本,你也可以手动导入别的系统的(posixpath,ntpath,m ...
- 使用Android绘图技术绘制一个椭圆形,然后通过触摸事件让该椭圆形跟着手指移动
引言:在图形绘制中,控制一个图形(圆形,椭圆形,矩形,三角形)移动时,其实计算的都是该图形的中心点移动.在绘制过程中,首先计算出中心点,然后根据中心点的位置计算重图形的上下左右位置.我们假设图形左边为 ...
- Android使用正则表达式验证手机号
国内手机号代码段分配如下: 移动:134.135.136.137.138.139.150.151.157(TD).158.159.187.188 联通:130.131.132.152.155.156. ...
- CAN总线学习系列之二——CAN总线与RS485的比较
CAN总线学习系列之二——CAN总线与RS485的比较 上 一节介绍了一下CAN总线的基本知识,那么有人会问,现在的总线格式很多,CAN相对于其他的总线有什么特点啊?这个问题问的好,所以我想与其它总线 ...
- 20155321 《网络攻防》 Exp8 Web基础
20155321 <网络攻防> Exp8 Web基础 基础问题回答 什么是表单? 表单是主要负责数据采集功能.主要是以下三个部分构成: 表单标签:包含处理表单数据所用的程序的URL以及数据 ...
- python 回溯法 子集树模板 系列 —— 1、8 皇后问题
问题 8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 分析 为了简化问题,考虑到8个皇后不同行,则每一行放置一个皇后,每一行的 ...
- Caffe上手教程
Caffe上手教程 入门系列FAQ72 在Unbuntu上安装Caffe828 Windows下安装Caffe1.4K Caffe框架上手教程1.2K Caffe编译运行调试462 Caffe 电脑配 ...