LeetCode Entry

1833. Maximum Ice Cream Bars

21.06.2026 medium 2026 kotlin rust

Max count by given coins

1833. Maximum Ice Cream Bars medium substack youtube

https://dmitrysamoylenko.com/leetcode/

21.06.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1397

Problem TLDR

Max count by given coins

Intuition

Maintain the frequency array. Calculate count by dividing total couns by price. Subtract count * price.

Approach

  • pre-calculate the max

Complexity

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

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

Code

    fun maxIceCream(c: IntArray, k: Int) = run {
        val s = c.groupBy{it}; var k = k
        (1..c.max()).sumOf {i->min(s[i]?.size?:0, k/i).also{k -= it*i}}
    }
    pub fn max_ice_cream(c: Vec<i32>, mut k: i32) -> i32 {
        let m=*c.iter().max().unwrap();let mut s=vec![0;m as usize+1];
        for x in c{s[x as usize]+=1}
        (1..=m).map(|i|{let r=s[i as usize].min(k/i);k-=r*i;r}).sum()
    }

Comments