You are given five integers cost1, cost2, costBoth, need1, and need2.
There are three types of items available:
cost1 and contributes 1 unit to the type 1 requirement only.cost2 and contributes 1 unit to the type 2 requirement only.costBoth and contributes 1 unit to both type 1 and type 2 requirements.You must collect enough items so that the total contribution toward type 1 is at least need1 and the total contribution toward type 2 is at least need2.
Return an integer representing the minimum possible total cost to achieve these requirements.
Example 1:
Input: cost1 = 3, cost2 = 2, costBoth = 1, need1 = 3, need2 = 2
Output: 3
Explanation:
After buying three type 3 items, which cost 3 * 1 = 3, the total contribution to type 1 is 3 (>= need1 = 3) and to type 2 is 3 (>= need2 = 2).
Any other valid combination would cost more, so the minimum total cost is 3.
Example 2:
Input: cost1 = 5, cost2 = 4, costBoth = 15, need1 = 2, need2 = 3
Output: 22
Explanation:
We buy need1 = 2 items of type 1 and need2 = 3 items of type 2: 2 * 5 + 3 * 4 = 10 + 12 = 22.
Any other valid combination would cost more, so the minimum total cost is 22.
Example 3:
Input: cost1 = 5, cost2 = 4, costBoth = 15, need1 = 0, need2 = 0
Output: 0
Explanation:
Since no items are required (need1 = need2 = 0), we buy nothing and pay 0.
Constraints:
1 <= cost1, cost2, costBoth <= 1060 <= need1, need2 <= 109Problem Overview: You must acquire a required number of items while minimizing the total cost. The challenge comes from multiple purchase options or pricing rules, where certain combinations or purchase sizes change the effective cost. The goal is to determine the cheapest strategy that still satisfies the required quantity.
Approach 1: Brute Force Enumeration (O(k) time, O(1) space)
The most direct way is to enumerate all reasonable purchase combinations and compute the total cost for each. For example, if there are bundle sizes or alternative purchase rules, iterate through how many times you apply each option and calculate the remaining items that must be bought individually. Each configuration produces a total cost, and you keep track of the minimum. This approach works because the number of realistic combinations is small and bounded. The downside is unnecessary repeated calculations when many combinations lead to the same effective purchase pattern.
This method is useful for validating logic during development or when constraints are tiny. It also helps reveal patterns that lead to a more optimized solution. The time complexity is O(k), where k represents the number of candidate purchase strategies you test, and space complexity remains O(1) since only a few counters and cost variables are stored.
Approach 2: Greedy Case Analysis (O(1) time, O(1) space)
A more efficient method relies on observing how the pricing rules interact. Instead of testing every possibility, analyze the structure of the pricing and derive a small number of meaningful cases. For example, compare the cost of buying items individually versus buying them in discounted groups. If a bundle provides a cheaper per‑item rate, maximize its usage; otherwise prefer individual purchases. In some scenarios you evaluate a few boundary cases such as "all bundles", "all individual", or "bundles plus remainder".
This turns the problem into a small mathematical comparison. You compute the total cost for each candidate case and return the minimum. Since the number of cases is constant, the algorithm runs in O(1) time and O(1) space. This pattern frequently appears in problems combining greedy decision making with small math calculations, where a local price advantage determines the optimal strategy.
Recommended for interviews: Greedy case analysis. Interviewers expect you to reason about the pricing structure and reduce the solution to a few deterministic scenarios instead of brute forcing every combination. Showing the brute force idea demonstrates understanding, but identifying the mathematical cases proves you can simplify the search space and derive an optimal O(1) solution.
We can divide the purchasing strategy into three cases:
a = need1 times cost1 + need2 times cost2.b = costBoth times max(need1, need2).mn = min(need1, need2), then the total cost is c = costBoth times mn + (need1 - mn) times cost1 + (need2 - mn) times cost2.Finally, we return the minimum value among the three cases, min(a, b, c).
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(k) | O(1) | When validating logic or when the number of possible purchase combinations is very small |
| Greedy Case Analysis | O(1) | O(1) | Preferred approach when pricing rules allow direct comparison between bundles and individual purchases |
Minimum Cost to Acquire Required Items | LeetCode 3789 | Weekly Contest 482 • Sanyam IIT Guwahati • 661 views views
Watch 3 more video solutions →Practice Minimum Cost to Acquire Required Items with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor