Maximum Number of Items From Sale I Problem
Description
LeetCode Problem 3946.
You are given a 2D integer array items, where items[i] = [factor_i, price_i] represents the i^th item. You are also given an integer budget.
There are unlimited copies of each item available for purchase.You may buy any number of copies of any items such that the total cost of the purchased copies is at most budget.
After buying items, you may receive free copies according to the following rules:
- For each item
ithat you bought at least one copy of, you receive one free copy of every itemjsuch thatj != iandfactor_idividesfactor_j. - Buying multiple copies of the same item
idoes not give additional free copies through itemi. - The same item
jcan be received multiple times for free if it is received from purchases of different item types.
Return the maximum total number of item copies you can obtain, including both purchased copies and free copies, while spending at most budget on purchased items.
Example 1:
Input: items = [[6,2],[2,6],[3,4]], budget = 9
Output: 4
Explanation:
- You can buy 2 copies of item 0 and 1 copy of item 2 for a total cost of 2 * 2 + 4 = 8, which is not greater than budget = 9.
- Buying item 2 gives 1 free copy of item 0, because factor_2 = 3 divides factor_0 = 6.
- You leave with 3 purchased copies and 1 free copy, for a total of 4 item copies.
Example 2:
Input: items = [[2,4],[3,2],[4,1],[6,4],[12,4]], budget = 8
Output: 10
Explanation:
- You can buy 1 copy of item 0, 1 copy of item 1, and 2 copies of item 2 for a total cost of 4 + 2 + 2 * 1 = 8.
- Buying item 0 gives 1 free copy of items 2, 3, and 4.
- Buying item 1 gives 1 free copy of items 3 and 4.
- Buying item 2 gives 1 free copy of item 4.
- Thus, you receive 6 free copies. You leave with 4 purchased copies and 6 free copies, for a total of 10 item copies.
Constraints:
1 <= items.length <= 1000items[i] = [factor_i, price_i]1 <= factor_i, price_i <= 15001 <= budget <= 1500
Solution
Approach: 0-1 Knapsack
Buying the first copy of an item i is special: it gives itself plus a free copy of every other item whose factor is a multiple of factor_i. Let cnt_i be the number of items whose factor is a multiple of factor_i, counting item i itself. The first copy of item i is worth cnt_i copies, and later copies only give 1 each.
So we treat the first copy of each item as a 0-1 knapsack object with cost price_i and value cnt_i, where f[j] is the best value using budget at most j. All remaining money buys extra copies of the cheapest item, mn, worth 1 each. We try every split: spend i on first copies and the rest on extras, and the answer is the maximum of f[i] + (budget - i) / mn.
Complexity: O(n2 + n m) time and O(m) space, where n is the number of items and m is the budget.
from math import inf
class Solution:
def maximumSaleItems(self, items: List[List[int]], budget: int) -> int:
f = [0] * (budget + 1)
mn = inf
for factor, price in items:
mn = min(mn, price)
cnt = sum(factor_j % factor == 0 for factor_j, _ in items)
for j in range(budget, price - 1, -1):
f[j] = max(f[j], f[j - price] + cnt)
return max(x + (budget - i) // mn for i, x in enumerate(f))