3947 - Maximum Number of Items From Sale II (Medium)
Problem Link
https://leetcode.com/problems/maximum-number-of-items-from-sale-ii
Problem Statement
You are given a 2D integer array items, where items[i] = [factori, pricei] represents the ith 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 != jandfactoridividesfactorj. - 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.
OriginalPython3Example 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 thanbudget = 19. - One purchased copy of item 0 gives 1 free copy of item 1, because
factor0 = 1dividesfactor1 = 2. - The other purchased copy of item 0 gives 1 free copy of item 2, because
factor0 = 1dividesfactor2 = 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 thanbudget = 35. - One purchased copy of item 0 gives 1 free copy of item 2, because
factor0 = 2dividesfactor2 = 6. - The other purchased copy of item 0 gives 1 free copy of item 3, because
factor0 = 2dividesfactor3 = 4. - The purchased copy of item 1 gives 1 free copy of item 2, because
factor1 = 1dividesfactor2 = 6. - Buying item 2 gives no free copy, because
factor2 = 6does 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 <= 1e5items[i] = [factori, pricei]1 <= factori <= items.length1 <= pricei <= 1e91 <= budget <= 1e9
Approach 1: Greedy
We want to count both the items we buy and the items we receive for free. When we buy one copy of an item, it may give us one additional free item. However, each item type can give each valid target item for free only once.
First, stores how many item types have factor . For example, if three items have factor , then is .
Next, we calculate . This is the number of item types whose factors are divisible by . To find it, we add the counts at , , , and so on. These are exactly the factors that an item with factor can give for free.
An item cannot give a free copy of itself, so an item with factor has possible free items. This means that its first purchased copies can each give one free copy. Every one of these purchases gives us two items in total: one purchased item and one free item.
After all free-item possibilities are used, buying more copies gives only one item. In that case, we should always buy the cheapest item available. We store its price in .
Buying one normal item costs , so buying two normal items costs . Therefore, a purchase that gives one free item is only better when its price is less than . We store these useful choices in .
We sort by price because cheaper purchases should be made first. For each choice, we buy as many copies as the budget allows, but no more than the number of free items that choice can produce. Each such purchase adds to . Finally, any money left is spent on the cheapest item. The number of additional items we can buy is , which is added to .
The time complexity is and the space complexity is .
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
div = [0] * (n + 1)
for f in range(1, n + 1): div[f] = sum(cnt[f::f])
c = min(p for _, p in items)
opt = sorted((p, div[f] - 1) for f, p in items if p < 2 * c)
res = 0
for p, k in opt:
x = min(k, budget // p)
res += 2 * x
budget -= p * x
return res + budget // c3946 - Maximum Number of Items From Sale I
Maximize the total number of purchased and free items within a budget using dynamic programming and factor-based rewards.
3963 - Create Grid With Exactly One Path
Construct a grid with exactly one valid path by opening the top row and rightmost column and blocking the remaining cells.