题目链接:

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的更多相关文章

  1. Week 9 - 638.Shopping Offers - Medium

    638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...

  2. LC 638. Shopping Offers

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  3. 【leetcode】638. Shopping Offers

    题目如下: In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, ther ...

  4. 【LeetCode】638. Shopping Offers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯法 日期 题目地址:https://le ...

  5. 638. Shopping Offers

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  6. Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)

    Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...

  7. 洛谷P2732 商店购物 Shopping Offers

    P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...

  8. poj 1170 Shopping Offers

    Shopping Offers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4696   Accepted: 1967 D ...

  9. USACO 3.3 Shopping Offers

    Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the ...

随机推荐

  1. 动态显示checkbox选中条数

    <script> $('input[type=checkbox]').click( function () { $('span#cheak_len').empty(); var len = ...

  2. python json格式字符串转换为字典格式

    不废话,看代码 #_*_ coding:utf- _*_ import os import json course=open('C:\\Users\\ly199\\Desktop\\list.txt' ...

  3. 学习Kali Linux必须知道的几点

    Kali Linux 在渗透测试和白帽子方面是业界领先的 Linux 发行版.默认情况下,该发行版附带了大量入侵和渗透的工具和软件,并且在全世界都得到了广泛认可.即使在那些甚至可能不知道 Linux ...

  4. Python中 __init__的通俗解释?附修饰器contextmanager的理解

    作者:匿名用户链接:https://www.zhihu.com/question/46973549/answer/103805810来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  5. Hadoop中ssh+IP、ssh+别名免秘钥登录配置

    1.为什么要进行 SSH 无密码验证配置? Hadoop运行过程中需要管理远端Hadoop守护进程,在Hadoop启动以后,NameNode是通过SSH(Secure Shell)来启动和停止各个Da ...

  6. ORACLE官网下载登陆账号能够使用

    username: responsecool@sina.com password: abc123ABC http://www.oracle.com/index.html

  7. Hadoop集群nodes unhealthy解决方法

    在搭建好Hadoop集群之后,所有服务均可正常启动,但是在运行MapReduce程序的时候,发现任务卡在7/09/07 22:28:14 INFO mapreduce.Job: Running job ...

  8. MSF里MS17_010利用模块笔记

    1.   auxiliary/scanner/smb/smb_ms17_010      //扫描检测漏洞辅助模块 扫描结果这里可以看到 2,3,4这几台主机存在此漏洞! 2.     auxilia ...

  9. Flutter - 添加从左向右滑动,返回上一个页面

    很多App比如微信.IT之家等都支持从屏幕左侧向右滑动,来返回上一个页面. 很多iOS上的App也都支持. 那么这个神奇的手势滑动是怎么实现的呢? 其实非常简单,只需要添加一句话即可. platfor ...

  10. Python3入门(七)——模块

    在Python中,一个.py文件就称之为一个模块(Module).(例如main.py就称之为main模块) 为了避免模块名冲突,Python又引入了按目录来组织模块的方法,称为包(Package). ...