Leetcode 2279 Maximum Bags With Full Capacity of Rocks Solution in java | Hindi Coding Community

0

 


You have n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and rocks. The ith bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also given an integer additionalRocks, the number of additional rocks you can place in any of the bags.


Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags.



public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
List<Integer> list = new ArrayList<>();
for(int i=0; i<rocks.length; i++){
list.add(capacity[i]-rocks[i]);
}
Collections.sort(list);
int ans=0,count=0;
for(int a:list){
count += a;
if(count > additionalRocks) break;
ans++;
}
return ans;
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !