|
| 1 | +--- |
| 2 | +id: Maximum-Bags-With-Full-Capacity-of-Rocks |
| 3 | +title: Maximum Bags With Full Capacity of Rocks |
| 4 | +sidebar_label: 2279-Maximum Bags With Full Capacity of Rocks |
| 5 | +tags: |
| 6 | + - Arrays |
| 7 | + - Brute Force |
| 8 | + - Optimized approach |
| 9 | + - LeetCode |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - C++ |
| 13 | + |
| 14 | +description: "This is a solution to Maximum Bags With Full Capacity of Rocks problem on LeetCode." |
| 15 | +sidebar_position: 80 |
| 16 | +--- |
| 17 | + |
| 18 | +## Problem Statement |
| 19 | +In this tutorial, we will solve the Maximum Bags With Full Capacity of Rocks problem . We will provide the implementation of the solution in Python, Java, and C++. |
| 20 | + |
| 21 | +### Problem Description |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags. |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | +Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2 |
| 31 | +Output: 3 |
| 32 | +Explanation: |
| 33 | +Place 1 rock in bag 0 and 1 rock in bag 1. |
| 34 | +The number of rocks in each bag are now [2,3,4,4]. |
| 35 | +Bags 0, 1, and 2 have full capacity. |
| 36 | +There are 3 bags at full capacity, so we return 3. |
| 37 | +It can be shown that it is not possible to have more than 3 bags at full capacity. |
| 38 | +Note that there may be other ways of placing the rocks that result in an answer of 3. |
| 39 | +**Example 2:** |
| 40 | +Input: capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100 |
| 41 | +Output: 3 |
| 42 | +Explanation: |
| 43 | +Place 8 rocks in bag 0 and 2 rocks in bag 2. |
| 44 | +The number of rocks in each bag are now [10,2,2]. |
| 45 | +Bags 0, 1, and 2 have full capacity. |
| 46 | +There are 3 bags at full capacity, so we return 3. |
| 47 | +It can be shown that it is not possible to have more than 3 bags at full capacity. |
| 48 | +Note that we did not use all of the additional rocks. |
| 49 | +### Constraints |
| 50 | +- `n == capacity.length == rocks.length` |
| 51 | +- `1 <= n <= 5 * 104` |
| 52 | +- `1 <= capacity[i] <= 109` |
| 53 | +- `0 <= rocks[i] <= capacity[i]` |
| 54 | +- `1 <= additionalRocks <= 109` |
| 55 | +## Solution of Given Problem |
| 56 | + |
| 57 | +### Intuition and Approach |
| 58 | + |
| 59 | +The problem can be solved using a brute force approach or an optimized Technique. |
| 60 | + |
| 61 | +<Tabs> |
| 62 | +<tabItem value="Brute Force" label="Brute Force"> |
| 63 | + |
| 64 | +### Approach 1:Brute Force (Naive) |
| 65 | + |
| 66 | + |
| 67 | +Brute Force Approach: The brute force approach involves trying every possible way to distribute the additional rocks among the bags to maximize the number of full bags. This approach is infeasible for large inputs due to its high complexity. |
| 68 | +#### Codes in Different Languages |
| 69 | + |
| 70 | +<Tabs> |
| 71 | +<TabItem value="C++" label="C++" default> |
| 72 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 73 | + |
| 74 | +```cpp |
| 75 | +#include <vector> |
| 76 | +#include <algorithm> |
| 77 | + |
| 78 | +int distributeRocks(std::vector<int>& capacity, std::vector<int>& rocks, int additionalRocks, int idx) { |
| 79 | + if (idx == capacity.size() || additionalRocks == 0) { |
| 80 | + int fullBags = 0; |
| 81 | + for (int i = 0; i < capacity.size(); ++i) { |
| 82 | + if (rocks[i] == capacity[i]) { |
| 83 | + ++fullBags; |
| 84 | + } |
| 85 | + } |
| 86 | + return fullBags; |
| 87 | + } |
| 88 | + |
| 89 | + int maxBags = distributeRocks(capacity, rocks, additionalRocks, idx + 1); |
| 90 | + |
| 91 | + for (int i = 1; i <= additionalRocks; ++i) { |
| 92 | + if (rocks[idx] + i <= capacity[idx]) { |
| 93 | + rocks[idx] += i; |
| 94 | + maxBags = std::max(maxBags, distributeRocks(capacity, rocks, additionalRocks - i, idx + 1)); |
| 95 | + rocks[idx] -= i; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return maxBags; |
| 100 | +} |
| 101 | + |
| 102 | +int maximumBagsBruteForce(std::vector<int>& capacity, std::vector<int>& rocks, int additionalRocks) { |
| 103 | + return distributeRocks(capacity, rocks, additionalRocks, 0); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +``` |
| 108 | +</TabItem> |
| 109 | +<TabItem value="Java" label="Java"> |
| 110 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 111 | +
|
| 112 | +```java |
| 113 | +import java.util.*; |
| 114 | +
|
| 115 | +public class BruteForceSolution { |
| 116 | + public int distributeRocks(int[] capacity, int[] rocks, int additionalRocks, int idx) { |
| 117 | + if (idx == capacity.length || additionalRocks == 0) { |
| 118 | + int fullBags = 0; |
| 119 | + for (int i = 0; i < capacity.length; ++i) { |
| 120 | + if (rocks[i] == capacity[i]) { |
| 121 | + ++fullBags; |
| 122 | + } |
| 123 | + } |
| 124 | + return fullBags; |
| 125 | + } |
| 126 | +
|
| 127 | + int maxBags = distributeRocks(capacity, rocks, additionalRocks, idx + 1); |
| 128 | +
|
| 129 | + for (int i = 1; i <= additionalRocks; ++i) { |
| 130 | + if (rocks[idx] + i <= capacity[idx]) { |
| 131 | + rocks[idx] += i; |
| 132 | + maxBags = Math.max(maxBags, distributeRocks(capacity, rocks, additionalRocks - i, idx + 1)); |
| 133 | + rocks[idx] -= i; |
| 134 | + } |
| 135 | + } |
| 136 | +
|
| 137 | + return maxBags; |
| 138 | + } |
| 139 | +
|
| 140 | + public int maximumBagsBruteForce(int[] capacity, int[] rocks, int additionalRocks) { |
| 141 | + return distributeRocks(capacity, rocks, additionalRocks, 0); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +
|
| 146 | +
|
| 147 | +``` |
| 148 | + |
| 149 | + |
| 150 | +</TabItem> |
| 151 | +<TabItem value="Python" label="Python"> |
| 152 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 153 | + |
| 154 | +```python |
| 155 | +def distribute_rocks(capacity, rocks, additional_rocks, idx): |
| 156 | + if idx == len(capacity) or additional_rocks == 0: |
| 157 | + full_bags = sum(1 for i in range(len(capacity)) if rocks[i] == capacity[i]) |
| 158 | + return full_bags |
| 159 | + |
| 160 | + max_bags = distribute_rocks(capacity, rocks, additional_rocks, idx + 1) |
| 161 | + |
| 162 | + for i in range(1, additional_rocks + 1): |
| 163 | + if rocks[idx] + i <= capacity[idx]: |
| 164 | + rocks[idx] += i |
| 165 | + max_bags = max(max_bags, distribute_rocks(capacity, rocks, additional_rocks - i, idx + 1)) |
| 166 | + rocks[idx] -= i |
| 167 | + |
| 168 | + return max_bags |
| 169 | + |
| 170 | +def maximum_bags_brute_force(capacity, rocks, additional_rocks): |
| 171 | + return distribute_rocks(capacity, rocks, additional_rocks, 0) |
| 172 | + |
| 173 | +``` |
| 174 | + |
| 175 | +</TabItem> |
| 176 | +</Tabs> |
| 177 | + |
| 178 | + |
| 179 | +### Complexity Analysis |
| 180 | + |
| 181 | +- Time Complexity: $O(2^n)$ |
| 182 | +- due to trying all combinations. |
| 183 | +- Space Complexity: $O(n)$ |
| 184 | +- for the recursive stack. |
| 185 | + |
| 186 | +</tabItem> |
| 187 | +<tabItem value="Optimized approach" label="Optimized approach"> |
| 188 | + |
| 189 | +### Approach 2: Optimized approach |
| 190 | + |
| 191 | +Optimized Approach: Calculate the difference between the capacity and the current number of rocks in each bag. |
| 192 | +Sort these differences in ascending order. |
| 193 | +Start filling the bags with the smallest difference first until the additional rocks are exhausted. |
| 194 | +Count the number of bags that have been filled to their capacity. |
| 195 | + |
| 196 | +#### Code in Different Languages |
| 197 | + |
| 198 | +<Tabs> |
| 199 | +<TabItem value="C++" label="C++" default> |
| 200 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 201 | + |
| 202 | +```cpp |
| 203 | +#include <vector> |
| 204 | +#include <algorithm> |
| 205 | + |
| 206 | +int maximumBags(std::vector<int>& capacity, std::vector<int>& rocks, int additionalRocks) { |
| 207 | + int n = capacity.size(); |
| 208 | + std::vector<int> spaceNeeded(n); |
| 209 | + |
| 210 | + // Calculate the space needed to fill each bag |
| 211 | + for (int i = 0; i < n; ++i) { |
| 212 | + spaceNeeded[i] = capacity[i] - rocks[i]; |
| 213 | + } |
| 214 | + |
| 215 | + // Sort the space needed in ascending order |
| 216 | + std::sort(spaceNeeded.begin(), spaceNeeded.end()); |
| 217 | + |
| 218 | + int fullBags = 0; |
| 219 | + |
| 220 | + // Fill the bags with the smallest space needed first |
| 221 | + for (int i = 0; i < n; ++i) { |
| 222 | + if (spaceNeeded[i] <= additionalRocks) { |
| 223 | + additionalRocks -= spaceNeeded[i]; |
| 224 | + ++fullBags; |
| 225 | + } else { |
| 226 | + break; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + return fullBags; |
| 231 | +} |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | +``` |
| 237 | +</TabItem> |
| 238 | +<TabItem value="Java" label="Java"> |
| 239 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 240 | +
|
| 241 | +```java |
| 242 | +import java.util.Arrays; |
| 243 | +
|
| 244 | +public class Solution { |
| 245 | + public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) { |
| 246 | + int n = capacity.length; |
| 247 | + int[] spaceNeeded = new int[n]; |
| 248 | +
|
| 249 | + // Calculate the space needed to fill each bag |
| 250 | + for (int i = 0; i < n; ++i) { |
| 251 | + spaceNeeded[i] = capacity[i] - rocks[i]; |
| 252 | + } |
| 253 | +
|
| 254 | + // Sort the space needed in ascending order |
| 255 | + Arrays.sort(spaceNeeded); |
| 256 | +
|
| 257 | + int fullBags = 0; |
| 258 | +
|
| 259 | + // Fill the bags with the smallest space needed first |
| 260 | + for (int i = 0; i < n; ++i) { |
| 261 | + if (spaceNeeded[i] <= additionalRocks) { |
| 262 | + additionalRocks -= spaceNeeded[i]; |
| 263 | + ++fullBags; |
| 264 | + } else { |
| 265 | + break; |
| 266 | + } |
| 267 | + } |
| 268 | +
|
| 269 | + return fullBags; |
| 270 | + } |
| 271 | +} |
| 272 | +
|
| 273 | +
|
| 274 | +``` |
| 275 | + |
| 276 | + |
| 277 | +</TabItem> |
| 278 | +<TabItem value="Python" label="Python"> |
| 279 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 280 | + |
| 281 | +```python |
| 282 | +def maximum_bags(capacity, rocks, additional_rocks): |
| 283 | + space_needed = [capacity[i] - rocks[i] for i in range(len(capacity))] |
| 284 | + |
| 285 | + # Sort the space needed in ascending order |
| 286 | + space_needed.sort() |
| 287 | + |
| 288 | + full_bags = 0 |
| 289 | + |
| 290 | + # Fill the bags with the smallest space needed first |
| 291 | + for space in space_needed: |
| 292 | + if space <= additional_rocks: |
| 293 | + additional_rocks -= space |
| 294 | + full_bags += 1 |
| 295 | + else: |
| 296 | + break |
| 297 | + |
| 298 | + return full_bags |
| 299 | + |
| 300 | + |
| 301 | + |
| 302 | +``` |
| 303 | + |
| 304 | +</TabItem> |
| 305 | +</Tabs> |
| 306 | + |
| 307 | +#### Complexity Analysis |
| 308 | + |
| 309 | +- Time Complexity: $O(n*logn)$ |
| 310 | +- due to sorting the differences. |
| 311 | +- Space Complexity: $O(n)$ |
| 312 | +- for storing the differences. |
| 313 | +- This approach is efficient and straightforward. |
| 314 | + |
| 315 | +</tabItem> |
| 316 | +</Tabs> |
| 317 | + |
| 318 | +--- |
| 319 | + |
| 320 | +<h2>Authors:</h2> |
| 321 | + |
| 322 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 323 | +{['AmruthaPariprolu'].map(username => ( |
| 324 | + <Author key={username} username={username} /> |
| 325 | +))} |
| 326 | +</div> |
0 commit comments