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第二十九课——文件读写(写数据的操作)
演示写数据的操作: 结论:往文件中写入数据,如果文件不存在,先创建文件,再写入内容 #1.打开文件 fw=open(r'd.txt','w',encoding='utf-8') #2.写数据操作 fw ...
- ORB SLAM2在Ubuntu 16.04上的运行配置
http://www.mamicode.com/info-detail-1773781.html 安装依赖 安装OpenGL 1. 安装opengl Library$sudo apt-get inst ...
- Moleskine智能笔+专用本:写完随时传到手机
http://www.totiot.com/61805.html Moleskine公司生产的速写本和速写板一直是涂鸦爱好者和速记员们的首选.该公司还联合Adobe. Livescribe. Ever ...
- 经实测解决Access-Control-Allow-Origin多域名跨域问题
网上流传2种方法解决多域名跨域问题:1.Access-Control-Allow-Origin:[域名一],[域名二]: 2.用$_SERVER("HTTP_ORIGIN")获取来 ...
- win10家庭版安装DockerToolbox-18.03.0-ce
下载DockerToolbox-18.03.0-ce.exe https://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 点击安 ...
- JAVA 框架hibernate (三)(数据库更新丢失)
一.场景: 我们在并发操作数据库同一个字段,比如:name:tom age:22这条数据.有2个同时进行操作.A操作该数据的name改成admin,B操作这条数据的age改成:35.然后A先把数据更 ...
- 树莓派学习笔记(2):常用linux命令
转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 1.ls命令:列出文件目录的常用命令,主要参数见下表. -a 列出目录下的所有文件,包括以.开头的隐含文件. -b ...
- DAY65
# 1.web应用# 2.CS和BS架构# 3.http协议# 特点: 1.接受请求正在处理# 2.请求处理完毕# 3.需要进行附加操作以完成请求# 4.服务器无法处理请求# 5.服务器处理请求出错# ...
- 配置Docker镜像加速器
由于在国内下载docker官方镜像的速度很慢且容易报错,因此需要配置docker镜像加速器来解决这个问题,加速器就是镜像代理的概念,只代理公有镜像,报错结果如下: net/http: request ...
- test zhenai
web.Document.InvokeScript("eval",new string[]{"document.getElementById('passwordbt'). ...