/**
* Problem: LeetCode 3947 - Maximum Number of Items From Sale II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
class Solution {
public int maximumSaleItems(int[][] items, int budget) {
int n = items.length;
// cnt[v] = kitne items ka factor v hai
int[] cnt = new int[n + 1];
for (int[] it : items) cnt[it[0]]++;
// m[d] = kitne items ka factor d ka multiple hai (Harmonic sum, O(n log n))
int[] m = new int[n + 1];
for (int d = 1; d <= n; d++) {
for (int x = d; x <= n; x += d) m[d] += cnt[x];
}
long minPrice = Long.MAX_VALUE;
long[] keys = new long[n];
for (int i = 0; i < n; i++) {
int f = m[items[i][0]] - 1; // khud ko chhodke
long price = items[i][1];
minPrice = Math.min(minPrice, price);
keys[i] = price * 131072L + f; // price ke hisaab se sort, f saath mein
}
Arrays.sort(keys);
long rem = budget;
long ans = 0;
for (long key : keys) {
long price = key / 131072L;
long f = key % 131072L;
// Value-2 copy tabhi faydemand hai jab price < 2 * minPrice
if (price >= 2 * minPrice) break;
if (f == 0) continue;
long take = Math.min(f, rem / price);
rem -= take * price;
ans += 2 * take;
if (take < f) break; // ab is price ya usse upar ka kuch afford nahi hoga
}
// Bacha hua budget sabse sasta item ki copies pe (value 1 each)
ans += rem / minPrice;
return (int) ans;
}
}
"""
Problem: LeetCode 3947 - Maximum Number of Items From Sale II
Difficulty: Medium
Language: Python 3
Author: CodeByTushu
"""
class Solution:
def maximumSaleItems(self, items: list[list[int]], budget: int) -> int:
n = len(items)
cnt = [0] * (n + 1)
for it in items:
cnt[it[0]] += 1
# m[d] = count of items whose factor is a multiple of d
m = [0] * (n + 1)
for d in range(1, n + 1):
for x in range(d, n + 1, d):
m[d] += cnt[x]
min_price = float('inf')
candidates = []
for it in items:
factor, price = it[0], it[1]
min_price = min(min_price, price)
f = m[factor] - 1
candidates.append((price, f))
candidates.sort(key=lambda x: x[0])
rem = budget
ans = 0
for price, f in candidates:
# Value-2 copy is strictly beneficial only when price < 2 * min_price
if price >= 2 * min_price:
break
if f == 0:
continue
take = min(f, rem // price)
rem -= take * price
ans += 2 * take
if take < f:
break
# Remaining budget spent on cheapest item (value 1 each)
ans += rem // min_price
return ans
/**
* Problem: LeetCode 3947 - Maximum Number of Items From Sale II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int maximumSaleItems(vector<vector<int>>& items, int budget) {
int n = items.size();
vector<int> cnt(n + 1, 0);
for (const auto& it : items) {
cnt[it[0]]++;
}
// m[d] = kitne items ka factor d ka multiple hai (Harmonic sum, O(n log n))
vector<int> m(n + 1, 0);
for (int d = 1; d <= n; d++) {
for (int x = d; x <= n; x += d) {
m[d] += cnt[x];
}
}
long long minPrice = LLONG_MAX;
vector<pair<long long, long long>> candidates;
candidates.reserve(n);
for (int i = 0; i < n; i++) {
long long price = items[i][1];
long long f = m[items[i][0]] - 1;
minPrice = min(minPrice, price);
candidates.push_back({price, f});
}
sort(candidates.begin(), candidates.end());
long long rem = budget;
long long ans = 0;
for (const auto& [price, f] : candidates) {
// Value-2 copy tabhi beneficial hai jab price < 2 * minPrice
if (price >= 2 * minPrice) break;
if (f == 0) continue;
long long take = min(f, rem / price);
rem -= take * price;
ans += 2 * take;
if (take < f) break;
}
// Bacha hua budget sabse sasta item ki copies pe
ans += rem / minPrice;
return (int)ans;
}
};
/**
* Problem: LeetCode 3947 - Maximum Number of Items From Sale II
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[][]} items
* @param {number} budget
* @return {number}
*/
var maximumSaleItems = function(items, budget) {
const n = items.length;
const cnt = new Int32Array(n + 1);
for (let i = 0; i < n; i++) {
cnt[items[i][0]]++;
}
const m = new Int32Array(n + 1);
for (let d = 1; d <= n; d++) {
for (let x = d; x <= n; x += d) {
m[d] += cnt[x];
}
}
let minPrice = Infinity;
const candidates = new Array(n);
for (let i = 0; i < n; i++) {
const price = items[i][1];
const f = m[items[i][0]] - 1;
if (price < minPrice) minPrice = price;
candidates[i] = [price, f];
}
candidates.sort((a, b) => a[0] - b[0]);
let rem = BigInt(budget);
let ans = 0n;
const minP = BigInt(minPrice);
const limit = 2n * minP;
for (let i = 0; i < n; i++) {
const p = BigInt(candidates[i][0]);
const f = BigInt(candidates[i][1]);
if (p >= limit) break;
if (f === 0n) continue;
const maxAfford = rem / p;
const take = f < maxAfford ? f : maxAfford;
rem -= take * p;
ans += 2n * take;
if (take < f) break;
}
ans += rem / minP;
return Number(ans);
};