Skip to content

Commit 3fad8b1

Browse files
add 638
1 parent 14e2ca0 commit 3fad8b1

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Your ideas/fixes/algorithms are more than welcome!
2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2323
|640|[Solve the Equation](https://leetcode.com/problems/solve-the-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_640.java) | O(n) |O(n) | Medium |
24+
|638|[Shopping Offers](https://leetcode.com/problems/shopping-offers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_638.java) | O(2^n) |O(n) | Medium | DP, DFS
2425
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_637.java) | O(n) |O(1) | Easy |
2526
|634|[Find the Derangement of An Array](https://leetcode.com/problems/find-the-derangement-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_634.java) | O(n) |O(1) | Medium | Math
2627
|633|[Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_633.java) | O(logn) |O(1) | Easy | Binary Search
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 638. Shopping Offers
8+
*
9+
* In LeetCode Store, there are some kinds of items to sell. Each item has a price.
10+
* However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
11+
* You are given the each item's price, a set of special offers,
12+
* and the number we need to buy for each item.
13+
* The job is to output the lowest price you have to pay for exactly certain items as given,
14+
* where you could make optimal use of the special offers.
15+
* Each special offer is represented in the form of an array,
16+
* the last number represents the price you need to pay for this special offer,
17+
* other numbers represents how many specific items you could get if you buy this offer.
18+
* You could use any of special offers as many times as you want.
19+
20+
Example 1:
21+
Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
22+
Output: 14
23+
Explanation:
24+
There are two kinds of items, A and B. Their prices are $2 and $5 respectively.
25+
In special offer 1, you can pay $5 for 3A and 0B
26+
In special offer 2, you can pay $10 for 1A and 2B.
27+
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
28+
29+
Example 2:
30+
Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
31+
Output: 11
32+
Explanation:
33+
The price of A is $2, and $3 for B, $4 for C.
34+
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C.
35+
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.
36+
You cannot add more items, though only $9 for 2A ,2B and 1C.
37+
38+
Note:
39+
There are at most 6 kinds of items, 100 special offers.
40+
For each item, you need to buy at most 6 of them.
41+
You are not allowed to buy more items than you want, even if that would lower the overall price.
42+
*/
43+
public class _638 {
44+
/**reference: https://leetcode.com/articles/shopping-offers/#approach-1-using-recursion-accepted*/
45+
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
46+
return shopping(price, special, needs, 0);
47+
}
48+
49+
public int shopping(List < Integer > price, List < List < Integer >> special, List < Integer > needs, int i) {
50+
if (i == special.size()) {
51+
return dot(needs, price);
52+
}
53+
ArrayList<Integer> clone = new ArrayList(needs);
54+
int j = 0;
55+
for (j = 0; j < special.get(i).size() - 1; j++) {
56+
int diff = clone.get(j) - special.get(i).get(j);
57+
if (diff < 0) break;
58+
clone.set(j, diff);
59+
}
60+
if (j == special.get(i).size() - 1) {
61+
return Math.min(special.get(i).get(j) + shopping(price, special, clone, i), shopping(price, special, needs, i + 1));
62+
} else {
63+
return shopping(price, special, needs, i + 1);
64+
}
65+
}
66+
67+
public int dot(List<Integer> a, List<Integer> b) {
68+
int sum = 0;
69+
for (int i = 0; i < a.size(); i++) {
70+
sum += a.get(i) * b.get(i);
71+
}
72+
return sum;
73+
}
74+
}

0 commit comments

Comments
 (0)