Description
In LeetCode Store, there are n 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 an integer array price where price[i] is the price of the ith item, and an integer array needs where needs[i] is the number of pieces of the ith item you want to buy.
You are also given an array special where special[i] is of size n + 1 where special[i][j] is the number of pieces of the jth item in the ith offer and special[i][n] (i.e., the last integer in the array) is the price of the ith offer.
Return the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.
Example 1:
Input: price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2] Output: 14 Explanation: There are two kinds of items, A and B. Their prices are 2 and5 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), and4 for 2A.
Example 2:
Input: price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1] Output: 11 Explanation: The price of A is 2, and3 for B, $4 for C. You may pay 4 for 1A and 1B, and9 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), and3 for 1B, $4 for 1C. You cannot add more items, though only $9 for 2A ,2B and 1C.
Constraints:
n == price.length == needs.length1 <= n <= 60 <= price[i], needs[i] <= 101 <= special.length <= 100special[i].length == n + 10 <= special[i][j] <= 50- The input is generated that at least one of
special[i][j]is non-zero for0 <= j <= n - 1.
Solutions
Solution 1: State Compression + Memoization Search
We notice that the number of types of items n ≤ 6 in the problem, and the quantity of each item needed does not exceed 10. We can use 4 binary bits to represent the quantity of each item needed. Thus, we only need at most 6 × 4 = 24 binary bits to represent the entire shopping list.
First, we convert the shopping list needs into an integer mask, where the quantity of the i-th item needed is stored in the i × 4 to (i + 1) × 4 - 1 bits of mask. For example, when needs = [1, 2, 1], we have mask = 0b0001 0010 0001.
Then, we design a function dfs(cur), representing the minimum amount of money we need to spend when the current state of the shopping list is cur. Therefore, the answer is dfs(mask).
The calculation method of the function dfs(cur) is as follows:
- First, we calculate the cost of the current shopping list cur without using any bundles, denoted as ans.
- Then, we iterate through each bundle offer. If the current shopping list cur can use the bundle offer, i.e., the quantity of each item in cur is not less than that in the bundle offer, then we can try to use this bundle. We subtract the quantity of each item in the bundle offer from cur, obtaining a new shopping list nxt, then recursively calculate the minimum cost of nxt and add the price of the bundle offer[n], updating ans, i.e., ans = min(ans, offer[n] + dfs(nxt)).
- Finally, return ans.
To avoid repeated calculations, we use a hash table f to record the minimum cost corresponding to each state cur.
The time complexity is O(n × k × m^n), where n represents the types of items, and k and m respectively represent the number of bundles and the maximum demand for each type of item. The space complexity is O(n × m^n).
class Solution: def shoppingOffers( self, price: List[int], special: List[List[int]], needs: List[int] ) -> int: @cache def dfs(cur: int) -> int: ans = sum(p * (cur >> (i * bits) & 0xF) for i, p in enumerate(price)) for offer in special: nxt = cur for j in range(len(needs)): if (cur >> (j * bits) & 0xF) < offer[j]: break nxt -= offer[j] << (j * bits) else: ans = min(ans, offer[-1] + dfs(nxt)) return ans bits, mask = 4, 0 for i, need in enumerate(needs): mask |= need << i * bits return dfs(mask)(code-box)
