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. Spring框架的事务管理相关的类和API

    1. PlatformTransactionManager接口 -- 平台事务管理器.(真正管理事务的类).该接口有具体的实现类,根据不同的持久层框架,需要选择不同的实现类! 2. Transacti ...

  2. css hover使用条件

    1:必须是其子元素才可以使用: 举例: css: 正确:.group-goodsList ul li:hover .msct{background-color: #ff4000;color: #FFF ...

  3. vue-awesome-swiper插件

    http://www.cnblogs.com/songrimin/p/6905136.html 这个地址不错 在上一些我的demo代码 第一步安装 npm install vue-awesome-sw ...

  4. Codeforces 701C. They Are Everywhere 思路题

    C. They Are Everywhere time limit per test: 2 seconds memory limit per test:256 megabytes input: sta ...

  5. UI设计,你为什么不能把标题做的更明显呢?

    在设计中标题常常被重视,标题即是文案信息的精华提炼,那么如何能把标题在很多文案信息中脱颖而出就是设计师所要做的工作,前面的文章说过对比可以凸显主题,这期是在对比合理的前提下更进一步的处理方法,我们可以 ...

  6. mstsc本地驱动器

    mstsc 可以把本地驱动器 挂载到服务器上

  7. Zookeeper 系列(五)Curator API

    Zookeeper 系列(五)Curator API 一.Curator 使用 Curator 框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象使用. (1) CuratorFramewo ...

  8. oracle 表分区例子

    oracle表分区详解-一步一步教你oracle分区表详解   .创建三个不同的表空间,模拟在不同磁盘上的保存不同范围的数据    create tablespace test01 datafile ...

  9. VS2010/MFC编程(对话框:模态对话框及其弹出过程)

    讲讲什么是模态对话框和非模态对话框,以及模态对话框怎样弹出. 一.模态对话框和非模态对话框 Windows对话框分为两类:模态对话框和非模态对话框. 模态对话框是这样的对话框,当它弹出后,本应用程序其 ...

  10. Event事件冒泡和事件捕获

    <!doctype html> <html lang="en"> <head> <meta charset="gb2312&qu ...