Maximum Number of Items From Sale II Problem
Description
LeetCode Problem 3947.
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:
- Each purchased copy of item
ican give you at most one free copy of another itemj. - The free item must satisfy
i != jandfactor_idividesfactor_j. - For each ordered pair
(i, j), you can receive a free copy of itemjfrom purchases of itemiat most once, regardless of how many copies of itemiyou buy. - 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 = [[1,6],[2,4],[3,5]], budget = 19
Output: 5
Explanation:
- You can buy 2 copies of item 0 and 1 copy of item 1 for a total cost of 2 * 6 + 4 = 16, which is not greater than budget = 19.
- One purchased copy of item 0 gives 1 free copy of item 1, because factor_0 = 1 divides factor_1 = 2.
- The other purchased copy of item 0 gives 1 free copy of item 2, because factor_0 = 1 divides factor_2 = 3.
- You leave with 3 purchased copies and 2 free copies, for a total of 5 item copies.
Example 2:
Input: items = [[2,8],[1,10],[6,6],[4,12],[5,20],[5,17]], budget = 35
Output: 7
Explanation:
- You can buy 2 copies of item 0, 1 copy of item 1, and 1 copy of item 2 for a total cost of 2 * 8 + 10 + 6 = 32, which is not greater than budget = 35.
- One purchased copy of item 0 gives 1 free copy of item 2, because factor_0 = 2 divides factor_2 = 6.
- The other purchased copy of item 0 gives 1 free copy of item 3, because factor_0 = 2 divides factor_3 = 4.
- The purchased copy of item 1 gives 1 free copy of item 2, because factor_1 = 1 divides factor_2 = 6.
- Buying item 2 gives no free copy, because factor_2 = 6 does not divide the factor of any other item.
- You leave with 4 purchased copies and 3 free copies, for a total of 7 item copies.
Constraints:
1 <= items.length <= 10^5items[i] = [factor_i, price_i]1 <= factor_i <= items.length1 <= price_i <= 10^91 <= budget <= 10^9
Solution
Approach: Greedy on Free Copies
Let d_i be the number of other items whose factor is a multiple of factor_i. Buying c copies of item i gives c purchased copies and min(c, d_i) free copies, because each copy can unlock one different item. So the first d_i copies of item i are worth 2 each, and every copy after that is worth 1.
Let mn be the cheapest price. Extra copies worth 1 are best bought at price mn, so the problem becomes: choose some copies worth 2 (item i offers up to d_i of them at price price_i) and spend the rest of the money on copies worth 1 at price mn.
The copies worth 2 should be taken from the cheapest item upward. Within one item, taking more copies either never hurts or never helps, depending on whether price_i < 2 * mn, so it is enough to look at the state after fully using each item (or running out of money). We sort items by price and, after each one, record 2 * count + left / mn. The answer is the largest value seen, starting from budget / mn.
To get all d_i quickly, count the items by factor and, for each factor f, sum the counts over its multiples f, 2f, 3f, ... (a sieve), then subtract 1 for the item itself.
Complexity: O(n log n) time and O(n) space.
class Solution:
def maximumSaleItems(self, items: List[List[int]], budget: int) -> int:
n = len(items)
cnt = [0] * (n + 1)
for f, _ in items:
cnt[f] += 1
# multiples[f] = number of items whose factor is a multiple of f
multiples = [0] * (n + 1)
for f in range(1, n + 1):
for g in range(f, n + 1, f):
multiples[f] += cnt[g]
mn = min(p for _, p in items)
# an item i can pass on at most multiples[f_i] - 1 free copies
order = sorted(items, key=lambda it: it[1])
ans = budget // mn
count = 0
left = budget
for f, p in order:
t = min(multiples[f] - 1, left // p)
count += t
left -= t * p
ans = max(ans, 2 * count + left // mn)
return ans