Leetcode
Dynamic Programming
638 Shopping Offers

1. Question

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:
1
Input:
2
[2,5], [[3,0,5],[1,2,10]], [3,2]
3
4
Output:
5
14
6
7
Explanation:
8
9
There are two kinds of items, A and B. Their prices are $2 and $5 respectively.
10
In special offer 1, you can pay $5 for 3A and 0B
11
In special offer 2, you can pay $10 for 1A and 2B.
12
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
Copied!
Example 2:
1
Input:
2
[2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
3
4
Output:
5
11
6
7
Explanation:
8
9
The price of A is $2, and $3 for B, $4 for C.
10
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C.
11
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.
12
You cannot add more items, though only $9 for 2A ,2B and 1C.
Copied!
Note:
  1. 1.
    There are at most 6 kinds of items, 100 special offers.
  2. 2.
    For each item, you need to buy at most 6 of them.
  3. 3.
    You are not allowed to buy more items than you want, even if that would lower the overall price.

2. Implementation

(1) Recursion
1
class Solution {
2
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
3
int cost = payWithoutOffer(price, needs);
4
5
int n = price.size();
6
7
for (List<Integer> offer : special) {
8
boolean isValid = true;
9
List<Integer> needsCopy = new ArrayList<>(needs);
10
11
for (int i = 0; i < n; i++) {
12
if (needsCopy.get(i) < offer.get(i)) {
13
isValid = false;
14
break;
15
}
16
needsCopy.set(i, needsCopy.get(i) - offer.get(i));
17
}
18
19
if (isValid) {
20
cost = Math.min(cost, offer.get(n) + shoppingOffers(price, special, needsCopy));
21
}
22
}
23
return cost;
24
}
25
26
public int payWithoutOffer(List<Integer> price, List<Integer> needs) {
27
int cost = 0;
28
for (int i = 0; i < price.size(); i++) {
29
cost += price.get(i) * needs.get(i);
30
}
31
return cost;
32
}
33
}
Copied!
(2) DFS + Memoization
1
class Solution {
2
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
3
Map<List<Integer>, Integer> map = new HashMap<>();
4
return payWithOffers(price, special, needs, map);
5
}
6
7
public int payWithOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs, Map<List<Integer>, Integer> map) {
8
if (map.containsKey(needs)) {
9
return map.get(needs);
10
}
11
12
int res = payWithoutOffer(price, needs);
13
int n = price.size();
14
15
for (List<Integer> offer : special) {
16
boolean isValid = true;
17
List<Integer> needsCopy = new ArrayList<>(needs);
18
19
for (int i = 0; i < n; i++) {
20
if (needsCopy.get(i) < offer.get(i)) {
21
isValid = false;
22
break;
23
}
24
needsCopy.set(i, needs.get(i) - offer.get(i));
25
}
26
27
if (isValid) {
28
res = Math.min(res, offer.get(n) + payWithOffers(price, special, needsCopy, map));
29
map.put(needsCopy, res);
30
}
31
}
32
return res;
33
}
34
35
public int payWithoutOffer(List<Integer> price, List<Integer> needs) {
36
int res = 0;
37
for (int i = 0; i < price.size(); i++) {
38
res += price.get(i) * needs.get(i);
39
}
40
return res;
41
}
42
}
Copied!

3. Time & Space Complexity

DFS + Memoization: 时间复杂度O(k ^(mn)), k是offer的数量, m是item的个数,n是每个item要买的个数,空间复杂度O(mn)?