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.
Input: price =[2,5], special =[[3,0,5],[1,2,10]], needs =[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 for3A and 0B
In special offer 2, you can pay $10 for1A and 2B.You need to buy 3A and 2B, so you may pay $10 for1A and 2B(special offer #2), and $4 for2A.
Input: price =[2,3,4], special =[[1,1,0,4],[2,2,1,9]], needs =[1,2,1]Output: 11Explanation: The price of A is $2, and $3 for B, $4 for C.You may pay $4 for1A and 1B, and $9 for2A ,2B and 1C.You need to buy 1A ,2B and 1C, so you may pay $4 for1A and 1B(special offer #1), and $3 for1B, $4 for1C.You cannot add more items, though only $9 for2A ,2B and 1C.
We want the minimum cost to buy all needed items, using special offers optimally. Since each offer can be used multiple times and the state space is small, we can use DFS with memoization to try all combinations efficiently.
classSolution {
val memo = mutableMapOf<List<Int>, Int>()
funshoppingOffers(price: List<Int>, special: List<List<Int>>, needs: List<Int>): Int {
return dfs(price, special, needs)
}
fundfs(price: List<Int>, special: List<List<Int>>, needs: List<Int>): Int {
memo[needs]?.let { returnit }
var ans = needs.indices.sumOf { price[it] * needs[it] }
for (offer in special) {
val nxt = needs.toMutableList()
var valid = truefor (i in needs.indices) {
if (nxt[i] < offer[i]) valid = false nxt[i] -= offer[i]
}
if (valid) ans = minOf(ans, offer.last() + dfs(price, special, nxt))
}
memo[needs] = ans
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
defshoppingOffers(price: list[int], special: list[list[int]], needs: list[int]) -> int:
memo: dict[tuple[int, ...], int] = {}
defdfs(needs: tuple[int, ...]) -> int:
if needs in memo:
return memo[needs]
ans = sum(p * n for p, n in zip(price, needs))
for offer in special:
nxt = tuple(n - o for n, o in zip(needs, offer[:-1]))
if all(x >=0for x in nxt):
ans = min(ans, offer[-1] + dfs(nxt))
memo[needs] = ans
return ans
return dfs(tuple(needs))
⏰ Time complexity: O(s * m^n), where s is the number of specials, m is the max need, n is the number of items. The state space is small due to constraints.