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 i can give you at most one free copy of another item j.
The free item must satisfy i != j and factori divides factorj.
For each ordered pair (i, j), you can receive a free copy of item j from purchases of item iat most once, regardless of how many copies of item i you buy.
The same item j can 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 factor0 = 1 divides factor1 = 2.
The other purchased copy of item 0 gives 1 free copy of item 2, because factor0 = 1 divides factor2 = 3.
You leave with 3 purchased copies and 2 free copies, for a total of 5 item copies.
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 factor0 = 2 divides factor2 = 6.
The other purchased copy of item 0 gives 1 free copy of item 3, because factor0 = 2 divides factor3 = 4.
The purchased copy of item 1 gives 1 free copy of item 2, because factor1 = 1 divides factor2 = 6.
Buying item 2 gives no free copy, because factor2 = 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 <= 105
items[i] = [factori, pricei]
1 <= factori <= items.length
1 <= pricei <= 109
1 <= budget <= 109
Solutions
Solution 1: Greedy
Thinking
\(n\) reaches \(10^5\) and the budget reaches \(10^9\), so a knapsack over the budget does not fit.
Buying one item can unlock at most one other item whose factor it divides, and each ordered pair can be used only once. The number of gifts one item can trigger is therefore the number of item types whose factor is a multiple of its factor, minus one.
One purchase plus one gift yields two items, and that is worthwhile only when the price is strictly less than the cost of two cheapest items. Cheaper pairs are better, so the candidates are taken in increasing price order.
Count every factor's gift quota by enumerating multiples, then sort by price. If the current quota cannot be filled, the remaining budget cannot pay for an equally expensive or costlier pair, so the scan stops and the rest of the budget buys the cheapest item.
Buying an item whose factor is \(f\) can earn one free item: the two items are different types, \(f\) divides the other factor, and each ordered pair is used at most once. Let \(\textit{freq}[f]\) be the number of item types whose factor equals \(f\), and let \(\textit{divCount}[f]\) be the number whose factor is a multiple of \(f\). An item of factor \(f\) yields at most \(\textit{divCount}[f]-1\) gifts. \(\textit{divCount}\) is filled by enumerating multiples.
Let \(p_{\min}\) be the lowest price. Paying \(p\) for one item and receiving one gift produces two items. Buying two copies of the cheapest item costs \(2p_{\min}\). Only items with \(p<2p_{\min}\) and a positive gift quota are useful, and they are bought in increasing order of \(p\). If the current quota is \(g\), buy \(t=\min(g,\lfloor\textit{budget}/p\rfloor)\) copies, add \(2t\) to the answer, and subtract \(tp\) from the budget. When \(t<g\), the remaining budget is less than \(p\). Later items are no cheaper, so the scan stops. The leftover budget is spent on items of price \(p_{\min}\).
The time complexity is \(O(n\log n)\), and the space complexity is \(O(n)\), where \(n\) is the number of item types.