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:

  1. There are at most 6 kinds of items, 100 special offers.
  2. For each item, you need to buy at most 6 of them.
  3. You are not allowed to buy more items than you want, even if that would lower the overall price.

Approach #1: DFS + Backtracking. [Java]

class Solution {
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
return shopping(price, special, needs, 0);
} public int shopping(List<Integer> price, List<List<Integer>> special, List<Integer> needs, int index) {
if (index == special.size()) {
return purchaseWithOriginalPrice(price, needs);
}
List<Integer> clone = new ArrayList<>(needs);
int i;
for (i = 0; i < special.get(index).size() - 1; ++i) {
int remain = needs.get(i) - special.get(index).get(i);
if (remain < 0) {
break;
} else {
clone.set(i, remain);
}
}
if (i == special.get(index).size() - 1) {
return Math.min(special.get(index).get(i) + shopping(price, special, clone, index),
shopping(price, special, needs, index+1));
} else {
return shopping(price, special, needs, index+1);
}
} public int purchaseWithOriginalPrice(List<Integer> price, List<Integer> needs) {
int sum = 0;
for (int i = 0; i < needs.size(); ++i) {
sum += needs.get(i) * price.get(i);
}
return sum;
}
}

  

Analysis:

The idea is very similar to combination sum. In combination sum where each element can be repeated, check each element to see if it can be used (in this case, is the sum doesn't exceed the target). If so, use it. Repeat this untill we get the result.

For this question, we check each promotion offers, if the offer can be used, use it. Repear the process and find the minimum result. In this question, the condition whether one offer can be used is the number of items in the offer doesn't exceed the needed number. Find the minimum among all combinations.

The thing to remeber, which alse happens in real life is that some special offers are actually more expensive than buying each individual item in the offers! Thus be smart and compare if buying directly is cheaper.

Reference:

https://leetcode.com/problems/shopping-offers/discuss/105212/Very-Easy-to-understand-JAVA-Solution-beats-95-with-explanation

https://github.com/ctfu/Leetcode

638. Shopping Offers的更多相关文章

  1. LeetCode 638 Shopping Offers

    题目链接: LeetCode 638 Shopping Offers 题解 dynamic programing 需要用到进制转换来表示状态,或者可以直接用一个vector来保存状态. 代码 1.未优 ...

  2. Week 9 - 638.Shopping Offers - Medium

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

  3. LC 638. Shopping Offers

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

  4. 【leetcode】638. Shopping Offers

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

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

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

  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. maven的配置及仓库的配置

    1.maven的配置 1.1.注意:电脑上需要安装jdk. 1.2.配置MAVEN_HOME,再在path中配置到bin这一层. (1)配置MAVEN_HOME:我的电脑--->右击---> ...

  2. db2 sql调优

    当我们发现某个SQL语句执行很慢时,可以通过查看它的访问计划来定位原因,如是否执行了合适的索引.是否采用了正确的连接方法等.但是我们发现很多用户对访问计划的生成和解释工具的使用存在很多疑惑,本文通过一 ...

  3. PS、AI、AE常用快捷键大全

    PS,AI,AE最常用的快捷键来了. 注意:Mac用户请自觉把Ctrl换成Command理解就行. 2017 Adobe Photoshop CC 快捷键 2017Adobe Illustrator快 ...

  4. apk签名方法

    生成签名文件: 1.右击项目管理器 选择 Export...  菜单: 2.在弹出的Export窗口中选择 Android->Export Android Application 后 next: ...

  5. 编译HBase1.0.0-cdh5.4.2版本

    1. 编译环境准备 Jdk:1.7.0_x Maven : 3.3.9 hbase: cdh5.4.2-release 2. 用idea打开项目 使用git clone得到HBase源码.打开git: ...

  6. 2018.09.22 atcoder Integers on a Tree(构造)

    传送门 先考虑什么时候不合法. 第一是考虑任意两个特殊点的权值的奇偶性是否满足条件. 第二是考虑每个点的取值范围是否合法. 如果上述条件都满足的话就可以随便构造出一组解. 代码: #include&l ...

  7. idea使用svn提交时出现错误Warning not all local changes may be shown due to an error

    参考于https://www.cnblogs.com/zhujiabin/p/6708012.html 解决方案: 1.File > Settings > Version Control ...

  8. PS各个工具的字母快捷键和英文全名

    选框-Marquee(M) 移动-move(V) 套索-Lasso(L) 魔棒-Wand(W) 喷枪-injection lance (J) 画笔-Brush (B) 铅笔-pencil(N) 橡皮图 ...

  9. Windows命令行参数(不断更新)

    这里先讲一下系统变量: 注意:一旦将路径加入到环境变量Path中,那么运行它下面的程序的时候就不用非得指定到目标路径中,直接键入命令就行了. 1.type命令:打开并读取文件里面的内容. C:\Use ...

  10. svn 创建tag

    1. 右键项目(源) 2. 选择复制到哪个路径(创建文件夹选项) 3. 选择哪个版本(源的) 4. 填写备注 5. 结束 使用:import  .合并等