forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1833.java
28 lines (26 loc) · 905 Bytes
/
_1833.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.fishercoder.solutions;
import java.util.TreeMap;
public class _1833 {
public static class Solution1 {
public int maxIceCream(int[] costs, int coins) {
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
for (int cost : costs) {
treeMap.put(cost, treeMap.getOrDefault(cost, 0) + 1);
}
int maxIceCream = 0;
for (int cost : treeMap.keySet()) {
if (cost * treeMap.get(cost) <= coins) {
maxIceCream += treeMap.get(cost);
coins -= cost * treeMap.get(cost);
} else {
while (coins > 0 && coins - cost >= 0) {
coins -= cost;
maxIceCream++;
}
break;
}
}
return maxIceCream;
}
}
}