Apple Redistribution into Boxes - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
apple = [1,3,2], capacity = [4,3,1,5,2]
›
Output:
2
💡 Note:
We need 1+3+2 = 6 apples total. Using boxes with capacity [5,4] gives us 9 capacity, which is enough. We need minimum 2 boxes.
Example 2 — Single Large Box
$
Input:
apple = [5,5,5], capacity = [2,4,2,7]
›
Output:
4
💡 Note:
Total apples = 15. Even the largest box (7) isn't enough alone. We need all boxes: 2+4+2+7 = 15.
Example 3 — Perfect Fit
$
Input:
apple = [1,1,1,1,1], capacity = [10]
›
Output:
1
💡 Note:
Total apples = 5. Single box with capacity 10 is more than enough.
Constraints
- 1 ≤ n, m ≤ 50
- 1 ≤ apple[i] ≤ 50
- 1 ≤ capacity[i] ≤ 50
Visualization
Tap to expand
Understanding the Visualization
1
Input
Apple packs [1,3,2] and box capacities [4,3,1,5,2]
2
Process
Sum apples (6), sort boxes [5,4,3,2,1], pick largest until sum ≥ 6
3
Output
Minimum 2 boxes needed (capacity 5+4=9 ≥ 6 apples)
Key Takeaway
🎯 Key Insight: Greedy algorithm works because using larger boxes first always leads to the minimum number of boxes needed.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code