Apple Redistribution into Boxes
EasyPrompt
You are given an array apple of size n and an array capacity of size m.
There are n packs where the ith pack contains apple[i] apples. There are m boxes as well, and the ith box has a capacity of capacity[i] apples.
Return the minimum number of boxes you need to select to redistribute these n packs of apples into boxes.
Note that, apples from the same pack can be distributed into different boxes.
Example 1:
Input: apple = [1,3,2], capacity = [4,3,1,5,2]
Output: 2
Explanation: We will use boxes with capacities 4 and 5.
It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.Example 2:
Input: apple = [5,5,5], capacity = [2,4,2,7]
Output: 4
Explanation: We will need to use all the boxes.
Constraints:
1 <= n == apple.length <= 501 <= m == capacity.length <= 501 <= apple[i], capacity[i] <= 50- The input is generated such that it's possible to redistribute packs of apples into boxes.
Approaches
2 approaches with complexity analysis and trade-offs.
The core idea is to first determine the total number of apples that need to be stored. Since apples from a single pack can be split among multiple boxes, we only need the sum of all apples. To use the minimum number of boxes, we should greedily pick the boxes with the largest capacities first. A straightforward way to achieve this is by sorting the capacity array in descending order and then picking boxes one by one until all apples are accommodated.
Algorithm
- Calculate
totalApplesby summing theapplearray. - If
totalApplesis 0, return 0. - Sort the
capacityarray in ascending order. - Initialize
boxesCount = 0. - Iterate through the
capacityarray from the last element to the first.- Subtract the current capacity from
totalApples. - Increment
boxesCount. - If
totalApplesis now less than or equal to 0, break the loop.
- Subtract the current capacity from
- Return
boxesCount.
Walkthrough
The algorithm proceeds in these steps:
- Calculate the total number of apples by summing all elements in the
applearray. Let's call thistotalApples. - If
totalApplesis zero, no boxes are needed, so we return 0. - Sort the
capacityarray. To easily access the largest capacities, we can sort it in ascending order and iterate from the end, or sort it in descending order and iterate from the beginning. - Initialize a
boxesCountto 0. Iterate through the sorted capacities (from largest to smallest). - In each iteration, subtract the current box's capacity from
totalApplesand incrementboxesCount. - Continue this process until
totalApplesis less than or equal to zero. - The final
boxesCountis the minimum number of boxes required.
import java.util.Arrays; class Solution { public int minimumBoxes(int[] apple, int[] capacity) { int totalApples = 0; for (int a : apple) { totalApples += a; } if (totalApples == 0) { return 0; } // Sort capacity in ascending order Arrays.sort(capacity); int boxesCount = 0; // Iterate from the end to use largest capacities first for (int i = capacity.length - 1; i >= 0; i--) { totalApples -= capacity[i]; boxesCount++; if (totalApples <= 0) { break; } } return boxesCount; }}Complexity
Time
O(n + m log m), where `n` is the length of `apple` and `m` is the length of `capacity`. Summing the apples takes O(n) time. Sorting the capacities takes O(m log m) time. The final loop takes at most O(m) time. The sorting step dominates the complexity.
Space
O(log m) or O(m), depending on the implementation of the sorting algorithm. In Java, `Arrays.sort` for primitive types uses a dual-pivot quicksort, which requires O(log m) space on average for the recursion stack.
Trade-offs
Pros
Simple to understand and implement.
It's a general solution that works regardless of the range of values in the
capacityarray.
Cons
The O(m log m) time complexity from sorting is not the most optimal solution possible given the problem's constraints.
Solutions
Solution
class Solution {public int minimumBoxes(int[] apple, int[] capacity) { Arrays.sort(capacity); int s = 0; for (int x : apple) { s += x; } for (int i = 1, n = capacity.length;; ++i) { s -= capacity[n - i]; if (s <= 0) { return i; } } }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.