博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode_638.Shopping Offers
阅读量:7155 次
发布时间:2019-06-29

本文共 3362 字,大约阅读时间需要 11 分钟。

 

 

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: 14Explanation: 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 0BIn 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: 11Explanation: 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.

1.最多6种物品,每种最多买6个。所以一开始想到的是,用一个整数的每一位来表示一种物品买了多少个,dp[666666]表示6种物品每种买6个。这样可以将问题转化为一个完全背包问题。

struct Item{    int val,cost;    Item(int v,int c):val(v),cost(c) {}    bool operator < (const Item& item)const    {        return item.cost>cost;    }};class Solution{public:    bool legal(int x)    {        while(x>0)        {            if(x%10>6)                return 0;            x/=10;        }        return 1;    }    bool bigger(int a, int b)    {        while(a>0&&b>0)        {            if(a%10 < b%10)                return 0;            a/=10;            b/=10;        }        if(a>0)            return 1;        else if(a==0 && b==0)            return 1;        else            return 0;    }    int shoppingOffers(vector
& price, vector
>& special, vector
& needs) { int all=0; for(int i=0,w=1; i
dp(all+1,INT_MAX/2); dp[0]=0; vector
items; for(int i=0,w=1; i
DP

但是发现这样做的问题在于状态数最多有666666个,只用背包的解法复杂度很高,虽然过了,但是耗时2000ms,且使用70+Mb的内存空间。

 

2.第二种解法是用dfs,这里使用类似向量的运算来处理special,price和needs,重载运算符后很方便且可读性高。并且,dfs搜索的状态数远小于前一种解法。

bool operator < (const vector
& v, const int x){ for(auto& i:v) if(i
& v1, const vector
& v2){ for(int i=0; i
v2[i]) return false; return true;}int operator * (const vector
& v1, const vector
& v2){ int ret=0; for(int i=0; i
operator -= (vector
& v1, const vector
& v2){ for(int i=0; i
operator += (vector
& v1, const vector
& v2){ for(int i=0; i
& price, vector
>& special, vector
& needs, int cost=0) { if(needs < 0) return INT_MAX; int ret = cost + price * needs; for(int i=0; i
dfs

 这种解法耗时8ms,使用不到10Mb的空间。

转载于:https://www.cnblogs.com/jasonlixuetao/p/10516737.html

你可能感兴趣的文章
常用的mysql操作
查看>>
转: spring静态注入
查看>>
C#反射机制
查看>>
[leetcode.com]算法题目 - Sqrt(x)
查看>>
Python自带的hmac模块
查看>>
2102 石子归并 2
查看>>
网络学习之路1
查看>>
How to configue session timeout in Hive
查看>>
js表单处理1——关于表单提交
查看>>
关于ajax请求status 200 却进入error 回调函数或显示跨域问题的解决方案及原因
查看>>
远程控制篇:在DELPHI程序中拨号上网
查看>>
教程-Delphi操作快捷键
查看>>
第一个Sprint冲刺第八天
查看>>
CodeIgniter Doctrine2基本使用(一)(转)
查看>>
abp中linq的应用
查看>>
c#程序设计原则
查看>>
evaluate-reverse-polish-notation
查看>>
大d的考题 找规律
查看>>
练习: 省市联动(Ajax)
查看>>
maven 创建后报错
查看>>