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. PAT 1064 朋友数(20)(代码)

    1064 朋友数(20 分) 如果两个整数各位数字的和是一样的,则被称为是"朋友数",而那个公共的和就是它们的"朋友证号".例如 123 和 51 就是朋友数, ...

  2. svn回退版本/取消修改

    取消对代码的修改分为两种情况:   第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [ ...

  3. 品味性能之道<一>:性能测试思维与误区

           <java performance><品悟性能优化 oracle><面向模式的软件架构-模式系统>读书笔记应用调优分享.      性能问题的解决,首 ...

  4. tmux配置与用法整理

    tmux常用命令与快捷键 http://www.111cn.net/sys/linux/59058.htm http://www.360doc.com/content/13/0709/13/13047 ...

  5. cxf soap rest webservice spring

    1. 导入 jar 包 2. 编写接口 3. 编写实现 4. 配置spring 配置文件 5. 配置web.xml servlet 6. 访问 package com.diancai.test; im ...

  6. 2018.10.22 bzoj4380: [POI2015]Myjnie(区间dp)

    传送门 区间dp好题. f[i][j][k]f[i][j][k]f[i][j][k]表示区间[i,j][i,j][i,j]最小值为kkk时的最大贡献. 然后可以枚举端点转移. 当时口胡到这儿就不会了. ...

  7. 2018.10.16 uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂优化dp)

    传送门 一道不错的矩阵快速幂优化dpdpdp. 设f[i][j][k][l]f[i][j][k][l]f[i][j][k][l]表示前iii轮第iii轮还有jjj个一滴血的,kkk个两滴血的,lll个 ...

  8. hdu-1070(水题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 题意:一个人喝牛奶,有三个原则: 1.牛奶的日期不超过6天,就是最多5天. 2.每次只喝200m ...

  9. MATLAB中的快捷键

    Ctrl + c  中止程序的运行,鼠标要点到命令窗内.

  10. NoSQL: Cassandra, HBase, RocksDB

    转自: http://www.linkedin.com/pulse/nosql-cassandra-hbase-rocksdb-siddharth-anand I've had the pleasur ...