Intuition
Each copy of an item can pull in one free copy whose factor it divides, so a copy that lands a free item is worth two for its price; take the cheapest of those deals first, then spend whatever is left on copies of the single cheapest item.
Explanation
Idea
We do not control the free copies by luck — we choose what to buy, and the free copies follow fixed rules. The key realisation is about a single item i: every copy we buy of it can pull in at most one free copy, and the items it can pull from are fixed — all items j (with j != i) whose factor is a multiple of factor_i. Call the size of that set free_i.
So buying copies of item i works in two tiers:
- the first
free_icopies each arrive with a brand-new free item, so they give 2 items forprice_i; - any copies beyond that give just themselves, so they give 1 item for
price_i.
Counting free_i for every item quickly
free_i is "how many items have a factor that is a multiple of factor_i", minus the item itself. Factors are between 1 and n, so first count how many items carry each factor value. Then for each value f, add up the counts at f, 2f, 3f, ... — a divisor sieve. That is O(n log n) in total, and free_i equals that total minus one.
Choosing what to buy
Let p be the cheapest price of any item. There are only two kinds of purchases worth making:
- a double deal — a copy of item
ithat also pulls a free copy: 2 items forprice_i(onlyfree_iof these exist per item); - a single — one copy of the cheapest item: 1 item for
p.
A double deal beats spending the same money on singles only while price_i is below 2 * p (two singles cost 2 * p and also give 2 items). So sort all the double deals by price, take the cheapest ones first while they are still worth it and the budget allows, then pour whatever money is left into singles of the cheapest item.
Walk-through (items = [[1,6],[2,4],[3,5]], budget = 19)
- Factor counts: factor 1 has 1 item, factor 2 has 1, factor 3 has 1. Divisor sieve: item 0 has factor 1, which divides 1, 2 and 3, so 3 items qualify, giving
free_0 = 3 - 1 = 2. Items 1 and 2 (factors 2 and 3) divide no other factor here, sofree_1 = free_2 = 0. - Cheapest price
p = 4. The only double deals come from item 0: two of them, each costing 6. Since6 < 2 * 4 = 8, they are worth taking. - Take both double deals: cost
2 * 6 = 12, giving 2 bought + 2 free = 4 copies. Budget left:19 - 12 = 7. - Fill with singles of the cheapest item (price 4):
7 / 4 = 1more copy. Total =4 + 1 = 5.
The answer is 5.
Complexity
- Time: O(n log n) — the divisor sieve and sorting the deals both dominate.
- Space: O(n) — the factor-count and sieve arrays.
Implementation
class Solution {
public int maximumSaleItems(int[][] items, int budget) {
int n = items.length;
// cnt[v] = how many items have factor exactly v (factors are in 1..n).
int[] cnt = new int[n + 1];
int pMin = Integer.MAX_VALUE;
for (int[] it : items) {
cnt[it[0]]++;
pMin = Math.min(pMin, it[1]);
}
// mult[f] = how many items have a factor that is a MULTIPLE of f
// (harmonic sieve: f, 2f, 3f, ...). O(n log n) overall.
int[] mult = new int[n + 1];
for (int f = 1; f <= n; f++) {
int total = 0;
for (int k = f; k <= n; k += f) total += cnt[k];
mult[f] = total;
}
// Each item yields up to (mult[factor] - 1) free copies. A copy that also
// pulls a free copy is a "double deal": 2 items for price. It beats two
// cheapest singles only while price < 2 * pMin, so take the cheapest
// beneficial deals first, then spend any leftover on pMin singles.
int[][] deals = new int[n][2];
int m = 0;
for (int[] it : items) {
int free = mult[it[0]] - 1;
if (free > 0) {
deals[m][0] = it[1];
deals[m][1] = free;
m++;
}
}
int[][] d = java.util.Arrays.copyOf(deals, m);
java.util.Arrays.sort(d, (a, b) -> Integer.compare(a[0], b[0]));
long totalItems = 0;
long remaining = budget;
for (int[] deal : d) {
long price = deal[0];
long free = deal[1];
if (2L * pMin <= price) break;
long take = remaining / price;
if (take >= free) take = free;
totalItems += take * 2;
remaining -= take * price;
if (take < free) break;
}
totalItems += remaining / pMin;
return (int) totalItems;
}
}