Shopping_Offers

来源:互联网 发布:51微投票系统源码 编辑:程序博客网 时间:2024/06/11 08:26

题目描述:

   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.
   (给定一组商品的价格prices, 一些组合购买的促销策略special(包含商品数目组合及其对应金额),以及需要购买的商品个数needs
    求在最优策略下,恰好购买needs所需的商品,需要的最少金额数.)
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.
   (最多六种商品,100种优惠,每种商品最多买6个,不允许购买多出需求的商品,即使可以令总价最小)


思路:利用递归排列各种优惠券组合,不断更新sum变量,找到最小值,难点在于下面的递归回溯可能不太好想。

public class Shopping_Offers {public static int shoppingOffers(List<Integer> price, List<List<Integer>> special, List <Integer> needs) {        return DP(price,special,needs);    }public static int DP(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {if(price.size() == 0||special.size() == 0||needs.size() == 0)return 0;//最后的结果int sum = 0;boolean flag = true;//当前需求全用零钱买需要的钱数for(int i=0;i<needs.size();i++){sum += price.get(i)*needs.get(i);}//排列各种组合for(int i=0;i<special.size();i++){List<Integer> mid = new ArrayList<Integer>(needs);for(int j=0;j<needs.size();j++){flag = true;int dif = mid.get(j)-special.get(i).get(j);if(dif<0){flag = false;break;}mid.set(j, dif);}if(flag){//special.get(i).get(size)是使用了这种类型优惠券花费的钱//DP(price,special,mid)就是使用了i优惠券之后剩余需求的各种情况的排列组合//最后无法使用优惠券时返回的sum就是使用零钱的金额int size = special.get(i).size()-1;sum = Math.min(sum,special.get(i).get(size)+DP(price,special,mid));}}        return sum;    }public static void main(String[] args) {List<Integer> price = new ArrayList<Integer>();price.add(2);price.add(3);price.add(4);List<List<Integer>> special = new ArrayList();List<Integer> special1 = new ArrayList<Integer>();special1.add(1);special1.add(1);special1.add(0);special1.add(4);List<Integer> special2 = new ArrayList<Integer>();special2.add(2);special2.add(2);special2.add(1);special2.add(9);special.add(special1);special.add(special2);List<Integer> needs = new ArrayList<Integer>();needs.add(1);needs.add(2);needs.add(1);System.out.println(shoppingOffers(price,special,needs));}}


原创粉丝点击