Skip to content

Commit 380816a

Browse files
authored
Create maximum-ice-cream-bars.cpp
1 parent d2144bf commit 380816a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/maximum-ice-cream-bars.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxIceCream(vector<int>& costs, int coins) {
7+
sort(begin(costs), end(costs));
8+
for (int i = 0; i < size(costs); ++i) {
9+
coins -= costs[i];
10+
if (coins < 0) {
11+
return i;
12+
}
13+
}
14+
return size(costs);
15+
}
16+
};

0 commit comments

Comments
 (0)