LeetCode Entry

3789. Minimum Cost to Acquire Required Items

11.11.2026 medium 2026 kotlin rust

Min cost to make n1,n2 using cost1,cost2 and cost both

3789. Minimum Cost to Acquire Required Items medium substack youtube

https://dmitrysamoylenko.com/leetcode/

11.11.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1539

Problem TLDR

Min cost to make n1,n2 using cost1,cost2 and cost both

Intuition

This is a corner cases problem. Choose between taking both separately, taking both together using cost both or the combinations.

Approach

  • we can pick pairs and choose min between c1+c2 and cb
  • then add the leftover

Complexity

  • Time complexity: \(O(1)\)

  • Space complexity: \(O(1)\)

Code

    fun minimumCost(c1: Int, c2: Int, cb: Int, n1: Int, n2: Int)=
    1L*min(n1,n2)*min(c1+c2,cb) +
    1L*(n1-min(n1,n2))*min(c1,cb) +
    1L*(n2-min(n1,n2))*min(c2,cb)
    pub fn minimum_cost(c1: i32, c2: i32, cb: i32, n1: i32, n2: i32) -> i64 {
        n1.min(n2) as i64 * cb.min(c1 + c2) as i64
        + (n1 - n2).abs() as i64 * cb.min(if n1 > n2 { c1 } else { c2 }) as i64
    }

Comments