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
Problem: 1833. Maximum Ice Cream Bars
Problem Link: leetcode.com/problems/maximum-ice-cream-bars/
Companies Asked: N/A
Approach:
Sort the costs.
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;
}
};