-
Notifications
You must be signed in to change notification settings - Fork 0
/
2279. Maximum Bags With Full Capacity of Rocks.java
53 lines (53 loc) · 1.7 KB
/
2279. Maximum Bags With Full Capacity of Rocks.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
Map<Integer,Integer> map=new TreeMap<Integer,Integer>();
int c=0;
for(int i=0;i<capacity.length;i++)
{
int diff=(capacity[i]-rocks[i]);
if(diff>0)
{
if(map.containsKey(diff))
{
int count=map.get(diff);
map.put(diff,count+1);
}
else
{
map.put(diff,1);
}
}
else
{
c++;
}
}
for(Map.Entry<Integer,Integer> ele:map.entrySet())
{
int i=ele.getValue();
if((additionalRocks-(i*ele.getKey()))>=0)
{
additionalRocks-=(i*ele.getKey());
c+=i;
continue;
}
while(i-->0)
{
if(additionalRocks>0)
{
if(ele.getKey()>0 && (additionalRocks-ele.getKey())>=0)
{
additionalRocks-=ele.getKey();
c++;
}
else{
return c;
}
}else{
return c;
}
}
}
return c;
}
}