Formatted question description: https://leetcode.ca/all/638.html
638. Shopping Offers (Medium)
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:
 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.
Companies:
Google
Related Topics:
Dynamic Programming, Depthfirst Search
Solution 1. DFS
DFS the special offers one by one. For each special offer, try:
 Don’t use it
 Use it once
 Use it twice
…
Until we don’t have enough need to take the offer.
The ending cases are:
 we traversed every special offer. For the remaining needs, buy them using
price
.  all the needs are
0
.
// OJ: https://leetcode.com/problems/shoppingoffers/
// Time: O(NS) where N is length of `needs` and S is length of `special`.
// Space: O(NS)
class Solution {
private:
int ans = INT_MAX;
void dfs(vector<int>& price, vector<vector<int>>& special, vector<int> needs, int index, int cost) {
if (index == special.size()) {
for (int i = 0; i < needs.size(); ++i) {
cost += needs[i] * price[i];
}
ans = min(ans, cost);
return;
}
bool done = true;
for (int n : needs) {
if (n) {
done = false;
break;
}
}
if (done) {
ans = min(ans, cost);
return;
}
auto &offer = special[index];
dfs(price, special, needs, index + 1, cost);
for (int i = 0; true; ++i) {
for (int j = 0; j < needs.size(); ++j) {
needs[j] = offer[j];
if (needs[j] < 0) return;
}
dfs(price, special, needs, index + 1, cost += offer.back());
}
}
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
dfs(price, special, needs, 0, 0);
return ans;
}
};
Java

class Solution { public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) { int itemsCount = price.size(); int totalOriginal = 0; for (int i = 0; i < itemsCount; i++) totalOriginal += price.get(i) * needs.get(i); int size = special.size(); for (int i = size  1; i >= 0; i) { List<Integer> curSpecial = special.get(i); boolean deleteFlag = false; int original = 0; for (int j = 0; j < itemsCount; j++) { int itemCount = curSpecial.get(j); if (itemCount > needs.get(j)) { deleteFlag = true; break; } else original += price.get(j) * itemCount; } int specialAmount = curSpecial.get(itemsCount); if (specialAmount >= original) deleteFlag = true; if (deleteFlag) special.remove(i); else { curSpecial.set(itemsCount, specialAmount  original); special.set(i, curSpecial); } } return backtrack(price, special, needs, totalOriginal); } public int backtrack(List<Integer> price, List<List<Integer>> special, List<Integer> needs, int total) { int totalNeeds = 0; for (int need : needs) totalNeeds += need; if (totalNeeds == 0) return total; int minTotal = total; int itemsCount = price.size(); for (List<Integer> curSpecial : special) { boolean flag = true; for (int i = 0; i < itemsCount; i++) { if (curSpecial.get(i) > needs.get(i)) { flag = false; break; } } if (flag) { for (int i = 0; i < itemsCount; i++) needs.set(i, needs.get(i)  curSpecial.get(i)); total += curSpecial.get(itemsCount); int nextTotal = backtrack(price, special, needs, total); minTotal = Math.min(minTotal, nextTotal); for (int i = 0; i < itemsCount; i++) needs.set(i, needs.get(i) + curSpecial.get(i)); total = curSpecial.get(itemsCount); } } return minTotal; } }

Todo

class Solution(object): def shoppingOffers(self, price, special, needs): """ :type price: List[int] :type special: List[List[int]] :type needs: List[int] :rtype: int """ return self.dfs(price, special, needs) def dfs(self, price, special, needs): local_min = self.directPurchase(price, needs) for spec in special: remains = [needs[j]  spec[j] for j in range(len(needs))] if min(remains) >= 0: local_min = min(local_min, spec[1] + self.dfs(price, special, remains)) return local_min def directPurchase(self, price, needs): total = 0 for i, need in enumerate(needs): total += price[i] * need return total