3789. Minimum Cost to Acquire Required Items Medium
1/**
2 * [3789] Minimum Cost to Acquire Required Items
3 *
4 * You are given five integers cost1, cost2, costBoth, need1, and need2.
5 * There are three types of items available:
6 *
7 * An item of type 1 costs cost1 and contributes 1 unit to the type 1 requirement only.
8 * An item of type 2 costs cost2 and contributes 1 unit to the type 2 requirement only.
9 * An item of type 3 costs costBoth and contributes 1 unit to both type 1 and type 2 requirements.
10 *
11 * 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.
12 * Return an integer representing the minimum possible total cost to achieve these requirements.
13 *
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">cost1 = 3, cost2 = 2, costBoth = 1, need1 = 3, need2 = 2</span>
17 * Output: <span class="example-io">3</span>
18 * Explanation:
19 * 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).<br data-end="229" data-start="226" />
20 * Any other valid combination would cost more, so the minimum total cost is 3.
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">cost1 = 5, cost2 = 4, costBoth = 15, need1 = 2, need2 = 3</span>
25 * Output: <span class="example-io">22</span>
26 * Explanation:
27 * We buy need1 = 2 items of type 1 and need2 = 3 items of type 2: 2 * 5 + 3 * 4 = 10 + 12 = 22.<br />
28 * Any other valid combination would cost more, so the minimum total cost is 22.
29 * </div>
30 * <strong class="example">Example 3:
31 * <div class="example-block">
32 * Input: <span class="example-io">cost1 = 5, cost2 = 4, costBoth = 15, need1 = 0, need2 = 0</span>
33 * Output: <span class="example-io">0</span>
34 * Explanation:
35 * Since no items are required (need1 = need2 = 0), we buy nothing and pay 0.
36 * </div>
37 *
38 * Constraints:
39 *
40 * 1 <= cost1, cost2, costBoth <= 10^6
41 * 0 <= need1, need2 <= 10^9
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-cost-to-acquire-required-items/
47// discuss: https://leetcode.com/problems/minimum-cost-to-acquire-required-items/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn minimum_cost(cost1: i32, cost2: i32, cost_both: i32, need1: i32, need2: i32) -> i64 {
53
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3789() {
65 }
66}
67Back
© 2026 bowen.ge All Rights Reserved.