hi, let Avik connect you.


** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.

** You can check all the posts on the Posts page.

** More about me in the About Me section.

Maximum Ice Cream Bars


  • Approach:

    1. Sort the costs.

    2. Now try to buy each of the ice cream until having enough coins for buying ith ice cream.


  • Time and Space Complexity:

      • Time Complexity: O(NlogN)

      • Space Complexity: O(1)


Code [C++]

class Solution {

public:

int maxIceCream(vector<int>& costs, int coins) {

sort(costs.begin(), costs.end());

int i = 0, n = costs.size();

while(i<n && coins >= costs[i]){

coins -= costs[i];

i++;

}

return i;

}

};