Leetcode Problem 1833. Maximum Ice Cream Bars

1833. Maximum Ice Cream Bars

Leetcode Solutions

Approach: Counting Sort (Greedy)

  1. Initialize n as the length of the costs array.
  2. Initialize m as the maximum cost in the costs array.
  3. Initialize icecreams as 0, representing the number of ice creams bought.
  4. Initialize costsFrequency as an array of size m + 1, with all values set to 0.
  5. Iterate over the costs array, incrementing the corresponding index in costsFrequency for each cost.
  6. Iterate over each possible cost from 1 to m. a. While there are ice creams with the current cost and the boy has enough coins, buy an ice cream and reduce the coins accordingly. b. Increment icecreams by the number of ice creams bought at the current cost.
  7. Return the value of icecreams.
UML Thumbnail

Approach: Sorting (Greedy)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...