Maximum Ice Cream Bars LeetCode solution in Java

1
class Solution {
2
public int maxIceCream(int[] costs, int coins) {
3

4
// Greedy Approach
5
// a. sort cost in increasing order
6

7
Arrays.sort(costs);
8

9
int count = 0;
10
for (int cost : costs) {
11

12
// b. check remainig coin is greater or equal than cuurent ice - cream cost
13
if (coins - cost >= 0) {
14
coins -= cost;
15
count++;
16
}
17
}
18

19
return count;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0