Minimum Cost to Acquire Required Items — A Greedy O(1) Solution
LeetCode problem · Difficulty: Easy-Medium · Tags: Greedy, Math
You need need1 units of type 1 and need2 units of type 2. There are three ways to buy them, and the cheapest combination is not always the obvious one. This post walks through how I arrived at the solution, step by step.
The Problem
You are given five integers: cost1, cost2, costBoth, need1, and need2.
- An item of type 1 costs
cost1and contributes 1 unit to the type 1 requirement only. - An item of type 2 costs
cost2and contributes 1 unit to the type 2 requirement only. - An item of type 3 costs
costBothand contributes 1 unit to both requirements.
Collect enough items so the type 1 contribution is at least need1 and the type 2 contribution is at least need2. Return the minimum possible total cost.
Constraints: 1 ≤ cost1, cost2, costBoth ≤ 106, 0 ≤ need1, need2 ≤ 109.
The Three Items at a Glance
Examples
| Input | Output | Why |
|---|---|---|
| cost1=3, cost2=2, costBoth=1, need1=3, need2=2 | 3 | Three type-3 items: 3 × 1 = 3. Covers need1=3 and need2=3 (≥ 2). |
| cost1=5, cost2=4, costBoth=15, need1=2, need2=3 | 22 | Combo is too expensive: 2 × 5 + 3 × 4 = 22. |
| cost1=5, cost2=4, costBoth=15, need1=0, need2=0 | 0 | Nothing needed, buy nothing. |
How I Arrived at the Solution
Before writing any code, I asked three questions. Each one kills a whole family of wrong approaches.
1. When is a type-3 item ever worth buying?
A type-3 item gives you one unit of type 1 and one unit of type 2. Buying those two units separately costs cost1 + cost2. So a combo only helps when:
costBoth < cost1 + cost2
If the combo costs the same or more than the two separate items, it is never useful — buy everything separately and you are done. This single check splits the problem in two.
2. How many type-3 items can we possibly use?
Each combo covers one unit of each requirement, so you run out of pairs after min(need1, need2) combos. Buying more than that means paying for a unit of a requirement that is already fulfilled. So the number of useful combos is at most min(need1, need2) — and when combos are cheap, we want exactly that many, because every one of them replaces a pair that would cost more separately.
3. What about the leftover?
After using min(need1, need2) combos, one requirement is fully covered and the other still has |need1 - need2| units left. For each leftover unit you have two choices: buy the single item at its own price, or buy another combo — it still gives you the one unit you need (the other unit is wasted, but that is fine). So each leftover unit costs min(own price, costBoth).
That is the whole insight: the problem is greedy, not dynamic programming. There is no state to explore — just one comparison, then arithmetic.
The Decision Flow
The Approach (Step by Step)
- Check whether combos help at all. If
costBoth ≥ cost1 + cost2, returncost1 * need1 + cost2 * need2. - Pair up the requirements. Buy
pairs = min(need1, need2)type-3 items, costingpairs * costBoth. - Cover the leftover. If
need1 > need2, the remainingneed1 - need2units of type 1 costmin(cost1, costBoth)each. Otherwise the remaining type-2 units costmin(cost2, costBoth)each. - Return the total.
The Solution (JavaScript)
/**
* @param {number} cost1
* @param {number} cost2
* @param {number} costBoth
* @param {number} need1
* @param {number} need2
* @return {number}
*/
var minimumCost = function (cost1, cost2, costBoth, need1, need2) {
const pairs = Math.min(need1, need2);
// A combo is only worth it if it beats buying the two items separately.
if (costBoth >= cost1 + cost2) {
return cost1 * need1 + cost2 * need2;
}
// Buy one combo for each pair we need.
let total = pairs * costBoth;
// Cover whatever is left over. One more combo also gives us the
// missing unit, so each leftover unit costs min(its own price, costBoth).
if (need1 > need2) {
total += (need1 - need2) * Math.min(cost1, costBoth);
} else {
total += (need2 - need1) * Math.min(cost2, costBoth);
}
return total;
};
Worked Example — Dry Run
Take cost1 = 3, cost2 = 4, costBoth = 5, need1 = 3, need2 = 2.
costBoth = 5 < cost1 + cost2 = 7→ combos are worth it.pairs = min(3, 2) = 2→ buy 2 combos: 2 × 5 = 10. Now we have 2 of each.- Leftover:
need1 - need2 = 1unit of type 1 →min(3, 5) = 3. - Total = 10 + 3 = 13.
Compare with the alternatives:
Why Greedy Is Optimal
Think of it as an exchange argument. Every feasible solution can be rearranged so that type-3 items are only used to cover matched pairs — a combo used for a lone leftover unit can be swapped for the cheaper of the single item and the combo without increasing the cost. Once combos are restricted to pairs, using fewer than min(need1, need2) of them (when costBoth < cost1 + cost2) means replacing a cheap combo with two separate items that cost more. So taking all possible pairs is optimal, and the leftover is handled independently at the cheapest per-unit price. No choice affects any other choice, which is exactly when greedy works.
Complexity
- Time: O(1) — a few comparisons and multiplications, no loops.
- Space: O(1) — only a couple of variables.
Edge Cases Worth Testing
need1 = 0, need2 = 0→ 0. Nothing to buy.costBothis huge (e.g. 106) → combos never used, pure separate purchase.costBoth = 1with large needs → buymax(need1, need2)combos; the extra units come free.need1 = need2→ no leftover at all; the answer is justneed1 * min(costBoth, cost1 + cost2).
Problem link: Minimum Cost to Acquire Required Items on LeetCode.