Maximum Ice Cream Bars LeetCode solution in JavaScript

1
var maxIceCream = function (costs, coins) {
2
costs.sort((a, b) => a - b);
3
let count = 0;
4

5
for (let i = 0; i < costs.length; i++) {
6
if (costs[i] <= coins) {
7
count++;
8
coins -= costs[i];
9
} else {
10
break; // a small optimization, end the loop early if coins go down to zero before we reach end of the length of costs.
11
}
12
}
13
return count;
14
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0