[LeetCode] 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:
- There are at most 6 kinds of items, 100 special offers.
- For each item, you need to buy at most 6 of them.
- You are not allowed to buy more items than you want, even if that would lower the overall price.
这道题说有一些商品,各自有不同的价格,然后给我们了一些优惠券,可以在优惠的价格买各种商品若干个,要求我们每个商品要买特定的个数,问我们使用优惠券能少花多少钱,注意优惠券可以重复使用,而且商品不能多买。那么我们可以先求出不使用任何商品需要花的钱数作为结果res的初始值,然后我们遍历每一个coupon,定义一个变量isValid表示当前coupon可以使用,然后遍历每一个商品,如果某个商品需要的个数小于coupon中提供的个数,说明当前coupon不可用,isValid标记为false。如果遍历完了发现isValid还为true的话,表明该coupon可用,我们可以更新结果res,对剩余的needs调用递归并且加上使用该coupon需要付的钱数。最后别忘了恢复needs的状态,参见代码如下:
解法一:
class Solution {
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int res = , n = price.size();
for (int i = ; i < n; ++i) {
res += price[i] * needs[i];
}
for (auto offer : special) {
bool isValid = true;
for (int j = ; j < n; ++j) {
if (needs[j] - offer[j] < ) isValid = false;
needs[j] -= offer[j];
}
if (isValid) {
res = min(res, shoppingOffers(price, special, needs) + offer.back());
}
for (int j = ; j < n; ++j) {
needs[j] += offer[j];
}
}
return res;
}
};
下面这种解法也是递归的写法,总的来说思路跟上面没有啥差别,应该不难理解,参见代码如下:
解法二:
class Solution {
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int res = inner_product(price.begin(), price.end(), needs.begin(), );
for (auto offer : special) {
vector<int> r = helper(offer, needs);
if (r.empty()) continue;
res = min(res, shoppingOffers(price, special, r) + offer.back());
}
return res;
}
vector<int> helper(vector<int>& offer, vector<int>& needs) {
vector<int> r(needs.size(), );
for (int i = ; i < needs.size(); ++i) {
if (offer[i] > needs[i]) return {};
r[i] = needs[i] - offer[i];
}
return r;
}
};
参考资料:
https://discuss.leetcode.com/topic/95201/c-solution
https://discuss.leetcode.com/topic/95200/simple-java-recursive-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Shopping Offers 购物优惠的更多相关文章
- LeetCode Shopping Offers
原题链接在这里:https://leetcode.com/problems/shopping-offers/description/ 题目: In LeetCode Store, there are ...
- 洛谷P2732 商店购物 Shopping Offers
P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...
- Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)
Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...
- LeetCode 638 Shopping Offers
题目链接: LeetCode 638 Shopping Offers 题解 dynamic programing 需要用到进制转换来表示状态,或者可以直接用一个vector来保存状态. 代码 1.未优 ...
- HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...
- LC 638. Shopping Offers
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- Week 9 - 638.Shopping Offers - Medium
638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...
- 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 ...
随机推荐
- 转载:解决微信OAuth2.0网页授权回调域名只能设置一个的问题
项目地址:https://github.com/HADB/GetWeixinCode 说明:微信项目很多,但是回调域名有限,经常使用,做个笔记. 解决微信OAuth2.0网页授权只能设置一个回调域名的 ...
- 事后诸葛亮——城市安全风险管理项目Postmortem结果
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 本系统希望实现快速识别危害因素,使工作人员对风险作出准确的评估.即让使用者熟悉潜在的危险因素,知道 ...
- Flask 应用最佳实践
一个好的应用目录结构可以方便代码的管理和维护,一个好的应用管理维护方式也可以强化程序的可扩展性 应用目录结构 假定我们的应用主目录是"flask-demo",首先我们建议每个应用都 ...
- bzoj千题计划108:bzoj1018: [SHOI2008]堵塞的交通traffic
http://www.lydsy.com/JudgeOnline/problem.php?id=1018 关键点在于只有两行 所以一个2*m矩形连通情况只有6种 编号即对应代码中的a数组 线段树维护 ...
- 第二篇:Python数据类型
一.引子 1.什么是数据? x= #是我们要存储的数据 2.为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字(整型,长整型,浮点型,复数) 字 ...
- javascript实现小鸟飞行轨迹
javascript实现小鸟飞行轨迹 代码如下:
- 基于ssm的poi反射bean实例
一:该例子是笔者在实际项目应用过程中,针对项目完成的一套基于poi的导入导出例子,其中一些与项目有关的代码大家直接替换成自己的需求即可. 二:笔者在项目中使用的是poi的XSSF,对应maven的po ...
- vue组件详解(四)——使用slot分发内容
一.什么是slot 在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer>& ...
- 隐藏Easy UI 中parent.$.modalDialog 的button
例子: buttons : [ { text : '关闭', handler : function() { parent.$.modalDialog.handler.dialog('close'); ...
- Apollo单向SSL认证(1)
参考链接:https://www.cnblogs.com/benwu/articles/4891758.html keytool -genkey -alias mybroker -keyalg RSA ...