[Leetcode] DP-- 638. 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.

Solution:

 

1. use DP:

initialize spending fromthe retail price,

recursively iterate the spending for the rest needs,

compare the retail and each offer and find the minimum spending

 (1) Define the subproblem

dp[needs] indicate the lowest price for the needs 

(2) Find the recursion (state transition function)

dp[needs]  = min(dp[needs], dp[needs-offer[:-1]) + offer[-1])

(3) Get the base case

dp[needs] = sum of needs and prices

 1   dp = {}
 2         def shopphelper(needsTpl):
 3             if needsTpl in dp:
 4                 return dp[needsTpl]
 5             #intialize
 6             dp[needsTpl] = sum(t*p for t,p in zip(needsTpl, price))
 7             #get each special offer
 8             for sp in special:
 9                 ntuples = tuple((t-s for t,s in zip(needsTpl, sp[:-1])))   #may use sp instead of sp[:-1], zip will be automatically pruning the rest unmatched
10                 if min(ntuples) < 0:         #more than not exact
11                     continue
12                 dp[needsTpl] = min(dp[needsTpl], shopphelper(ntuples) + sp[-1])
13             return dp[needsTpl]
14
15         return shopphelper(tuple(needs))
16             

      

时间: 2024-10-11 06:43:55

[Leetcode] DP-- 638. Shopping Offers的相关文章

[Leetcode]638. Shopping Offers

链接:LeetCode638 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品. 现给定每个物品的价格,每个大礼包包含物品的清单,以及待购物品清单.请输出确切完成待购清单的最低花费. 每个大礼包的由一个数组中的一组数据描述,最后一个数字代表大礼包的价格,其他数字分别表示内含的其他种类物品的数量. 任意大礼包可无限次购买. 示例 1: 输入: \([2,5], [[3,0,5],[1,2,10]], [3,2]\) 输出: \(14\)

洛谷P2732 商店购物 Shopping Offers

P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 题目描述 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是

USACO 3.3 Shopping Offers

Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more customers, the shop introduces some special offers. A spe

背包系列练习( hdu 2844 Coins &amp;&amp; hdu 2159 &amp;&amp; poj 1170 Shopping Offers &amp;&amp; hdu 3092 Least common multiple &amp;&amp; poj 1015 Jury Compromise)

作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢http://blog.csdn.net/eagle_or_snail/article/details/50987044,这里有大部分比较有趣的dp练手题. hdu 2844 Coins 多重背包 就是一个10w的多重背包,每个物品的cost同时也作为value去做背包,我们求的是每个容量下的价值,所以没

【POJ 1170】 Shopping Offers [动态规划 状态压缩 背包][离散化]

POJ - 1170 Shopping Offers 放假打题 sufu 看完题我是懵比的 这....  emmmmm   瓜想了半个小时之后我选择狗带 然后点开链接 装压+dp!!!!哦!!!!!!巧妙!!!! 就先把目标状态还有各个优惠的状态处理好 然后就是一个完全背包处理用优惠 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int S=100+5,N=10,P=10000+5,C=1000+5; 4 int sale[S

Shopping Offers

Shopping Offers 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是 12z. 编写一个程序,计算顾客购买一定商品的花费,尽量利用优惠使花费最少.尽管有时候添加其他商品可以获得更少的花费,但是你不能这么做. 对于上面的商品信息,购买三朵花

POJ - 1170 Shopping Offers (五维DP)

题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来表示.每一个维度都代表一件商品的数量 打折的方式事实上有b + s种.将每种商品单件卖的也算一种打折方式 这题有个坑点,就是b或者s有可能为0 #include<cstdio> #include<cstring> #include<algorithm> #include&l

poj - 1170 - Shopping Offers(状态压缩dp)

题意:b(0 <= b <= 5)种物品,每种有个标号c(1 <= c <= 999),有个需要购买的个数k(1 <= k <=5),有个单价p(1 <= p <= 999),有s(0 <= s <= 99)种组合优惠方案,问完成采购最少需要多少钱. 题目链接:http://poj.org/problem?id=1170 -->>已有b种物品,再将每种优惠分别看成一种新物品,剩下就是完全背包问题了.. 设dp[i]表示购买状态为 i

poj - 1170 - Shopping Offers(减少国家dp)

意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 <= p <= 999).有s(0 <= s <= 99).问完毕採购最少须要多少钱. 题目链接:http://poj.org/problem?id=1170 -->>已有b种物品,再将每种优惠分别看成一种新物品,剩下就是全然背包问题了.. 设dp[i]表示购买状态为 i 时