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] os.path模块常用方法汇总
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
- js面对对象编程(二):属性和闭包
上篇博客中解说了一些js对象的基本概念和使用方法.这篇博客解说一下js属性方面的:公有属性.私有属性,特权方法. 假设学过java.公有属性.私有属性,特权方法(即能够訪问和设置私有属性的方法)一定非 ...
- SAP跟踪前台操作导致的后台查询语句
SAP跟踪前台操作导致的后台查询语句,通过这个可以查看前台对应了后台的数据库表,然后可以通过se11查看表内容,也可以删除表内容. 在sap升级的时候,首先需要拷贝正式的sap系统,然后将拷贝的系统中 ...
- 难度并不NOIP的NOIP模拟赛
今天老师请了前几届的学长来讲课,可是讲课为什么要考试呢... 学长说难度是NOIP,于是我就naive的跟着参加了,然而T3难度并不友好,感觉确实不是很适合我们现在做......不过课本来也不是给我们 ...
- jQuery 实现复选框的全选与反选
<script> //实现全选与反选 $(".allAndNotAll").click(function () { if ($(this).prop("che ...
- Mysql表创建外键报错
数据库表A: CREATE TABLE task_desc_tab ( id INT(11) PRIMARY KEY NOT NULL COMMENT '自增主键' AUTO_INCREMENT, t ...
- 离线安装Cloudera Manager 5和CDH5(最新版5.9.3) 完全教程(二)基础环境安装
一.安装CentOS 6.5 x64 具体安装过程自行百度 1.1 修改IP地址 [root@master ~]# vi /etc/sysconfig/network DEVICE=eth0 TYPE ...
- ConsenSys/eth-lightwallet(browserless)
https://github.com/ConsenSys/eth-lightwallet LightWallet A minimal ethereum javascript wallet.一个小型的钱 ...
- linux IP 命令使用举例(转)
ip 1.作用ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig.route等,使用权限为超级用户.几乎所有的Linux发行版本都支持该命 ...
- shiro实战系列(二)之入门实战续
下面讲解基于实战系列一,所以相关的java文件获取pom.xml及其log4j文件同样适用于本次讲解. 一.Using Shiro Using Shiro 现在我们的 SecurityManager ...