# Solution For Maximum Bags With Full Capacity Of Rocks

The Maximum Bags with Full Capacity of Rocks problem on LeetCode is a binary search problem that asks to find the maximum number of bags that can hold rocks of a given weight capacity. The problem gives an array of rocks with their weights and the capacity of each bag.

The approach to solving the problem is as follows:

1. Initialize the left pointer to 1 and the right pointer to the sum of all rocks.

2. While the left pointer is less than or equal to the right pointer, perform the following steps:

a. Calculate the mid value between the left and right pointers.

b. Initialize a variable to keep track of the number of bags.

c. Initialize a variable to keep track of the current weight capacity of the bag.

d. Iterate through the rocks array and for each rock, check if adding its weight to the current capacity of the bag exceeds the mid value.

If it does, increment the bag count and reset the current weight capacity to the weight of the current rock. Otherwise, add the weight of the current rock to the current weight capacity of the bag.

e. If the number of bags required is less than or equal to the given number of bags, update the left pointer to mid + 1. Since we want to maximize the number of bags, we can try to increase the weight capacity.

f. If the number of bags required is greater than the given number of bags, update the right pointer to mid – 1. Since we want to minimize the weight capacity, we can try to decrease the number of bags.

1. Return the left pointer – 1, which represents the maximum number of bags that can hold rocks of the given weight capacity.

Here is the Python code for the solution:

`class Solution: def maximumNumberOfBags(self, nums: List[int], maxCapacity: int) -> int: left, right = 1, sum(nums) while left <= right: mid = (left + right) // 2 bags = 0 capacity = 0 for num in nums: if capacity + num > mid: bags += 1 capacity = num else: capacity += num if bags + 1 <= maxCapacity: left = mid + 1 else: right = mid - 1 return left - 1`

Time Complexity: O(n log sum), where n is the number of rocks and sum is the sum of all rocks.

Space Complexity: O(1)

## Step by Step Implementation For Maximum Bags With Full Capacity Of Rocks

```class Solution {
public int maxBags(int capacity, int[] weights) {
// check for invalid inputs
if (capacity <= 0 || weights == null || weights.length == 0) {
return 0;
}

// sort the weights in descending order
Arrays.sort(weights);

// start from the heaviest weight
int numBags = 0;
int i = weights.length - 1;
while (i >= 0) {
// if the weight can be carried in the current bag
if (weights[i] <= capacity) {
numBags++;
// reduce the remaining capacity
capacity -= weights[i];
} else {
// go to the next heaviest weight
i--;
}
}

return numBags;
}
}```
```def max_bags(rocks, capacity):
"""
>>> max_bags([2, 4, 6, 8], 10)
3
>>> max_bags([2, 4, 6, 8], 11)
4
"""

# Sort the rocks in reverse order
rocks = sorted(rocks, reverse=True)

# Initialize max_bags to 0
max_bags = 0

# Iterate through the rocks
for rock in rocks:

# If the current rock is less than or equal to the capacity
if rock <= capacity:

max_bags += 1

# Subtract the weight of the rock from the capacity
capacity -= rock

# Return max_bags
return max_bags```
```var maximumBagsWithFullCapacityOfRocks = function(rocks, capacity) {
};```
```The problem is as follows:

You have a bag with a capacity of N rocks. You also have an unlimited supply of rocks. Your goal is to fill the bag to capacity with the maximum number of rocks possible.

You can only put one rock in the bag at a time. You cannot put a rock in the bag if doing so would exceed the bag's capacity.

Your solution should be efficient in both time and space.

Here is one possible solution:

1. Start by putting a rock in the bag.

2. If adding another rock would exceed the bag's capacity, stop. Otherwise, add another rock to the bag.

3. Repeat step 2 until the bag is full or there are no more rocks left.

This solution is efficient in both time and space. It only takes O(N) time to fill the bag, and the space complexity is O(1) since we only need to keep track of the bag's capacity and the number of rocks.```
```public class Solution {
public int MaxBags(int[][] rocks, int capacity) {
// sort the rocks by weight in ascending order
Array.Sort(rocks, (a, b) => a - b);

// dp[i] is the maximum number of rocks that can be carried with the full capacity of i
int[] dp = new int[capacity + 1];
dp = 0;

for (int i = 0; i < rocks.Length; i++) {
int weight = rocks[i], value = rocks[i];

// for each weight, we can either carry 0 rocks of that weight, or 1 rock of that weight
// we want to carry the maximum value of rocks while staying under the capacity
for (int j = capacity; j >= weight; j--) {
dp[j] = Math.Max(dp[j], dp[j - weight] + value);
}
}

return dp[capacity];
}
}```

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]