Intuition
The free copies an item gives are fixed once you own it, so buying the first copy carries a bonus and extra copies add one each — a knapsack where every item can be skipped, activated once, or activated and then stocked up.
Explanation
Key idea
For every item i, the number of free copies it hands you is fixed the moment you buy at least one copy of it. Let free[i] be the count of other items j (j != i) whose factor is divisible by factor_i. Buying more copies of i never changes free[i].
So buying copies of item i splits into two kinds of purchase:
- The first copy costs
price_iand yields1 + free[i]item copies (the copy itself plus every free copy it unlocks). - Each extra copy costs
price_iand yields exactly1more copy.
That is a knapsack: pick how many copies k >= 0 of each item to buy. If k = 0 you gain nothing; if k >= 1 you spend k * price_i and gain k + free[i] copies.
Algorithm
- Compute
free[i]for every item. - Let
dp[b]= the most copies obtainable with budget at mostb, starting all zeros. - Process items one at a time. For item
iwith pricepand bonusfree[i], fold its choices intodpusing a helperg[b]= best copies when at least one copy ofiis bought with budgetb:g[b] = max(prev[b - p] + 1, g[b - p] + 1)— either this is the first copy (takeprev[b - p], the table before itemi) or another copy on top of an already-activated item (takeg[b - p]).dp[b] = max(prev[b], g[b] + free[i])— skip itemi, or activate it and add its free bonus once.
- The answer is
dp[budget].
Walk-through
Take items = [[6,2],[2,6],[3,4]], budget = 9.
First the bonuses: factor_0 = 6 divides no other factor, so free[0] = 0. factor_1 = 2 divides 6, so free[1] = 1. factor_2 = 3 divides 6, so free[2] = 1.
Now fill the budget table. After folding in item 0 (price 2, bonus 0), dp[b] is simply b // 2 — buy as many price-2 copies as fit. After item 2 (price 4, bonus 1) is also available, the best plan for budget = 9 is to buy item 0 twice and item 2 once: cost 2*2 + 4 = 8, giving 3 purchased copies, and item 2's bonus adds 1 free copy of item 0. Total 4 copies, and no plan beats it.
dp[9] = 4.
Complexity
- Time: O(n^2 + n * budget) — the n^2 is the pairwise divisibility scan for free[i]; the knapsack is n * budget.
- Space: O(budget) for the rolling table.
Implementation
class Solution {
public int maximumSaleItems(int[][] items, int budget) {
int n = items.length;
// free[i] = number of OTHER items j whose factor is divisible by factor_i.
int[] free = new int[n];
for (int i = 0; i < n; i++) {
int fi = items[i][0];
int count = 0;
for (int j = 0; j < n; j++) {
if (j != i && items[j][0] % fi == 0) count++;
}
free[i] = count;
}
final int NEG = Integer.MIN_VALUE / 2;
// dp[b] = max copies obtainable with budget at most b.
int[] dp = new int[budget + 1];
for (int i = 0; i < n; i++) {
int price = items[i][1];
int bonus = free[i];
int[] prev = dp.clone(); // table before considering item i
int[] g = new int[budget + 1]; // g[b] = best copies if >=1 copy of i bought
java.util.Arrays.fill(g, NEG);
for (int b = price; b <= budget; b++) {
// First copy of i (extend prev[b - price]) or another copy (extend g[b - price]).
int best = prev[b - price] + 1;
if (g[b - price] != NEG && g[b - price] + 1 > best) {
best = g[b - price] + 1;
}
g[b] = best;
// Skip item i, or activate it and add its free bonus exactly once.
if (best + bonus > dp[b]) dp[b] = best + bonus;
}
}
return dp[budget];
}
}