3074. Apple Redistribution into Boxes
UnknownView on LeetCode
Time: O(n log C)
Space: O(1)
Problem Overview
Apple Redistribution into Boxes (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Greedy pattern in coding interviews. Binary search on box capacity; validate smallest boxes can hold all grouped apples.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Binary search on box capacity; validate smallest boxes can hold all grouped apples.
3074.cs
C#
// Approach: Binary search on box capacity; validate smallest boxes can hold all grouped apples.
// Time: O(n log C) Space: O(1)
public class Solution
{
public int MinimumBoxes(int[] apple, int[] capacity)
{
int appleSum = apple.Sum();
int capacitySum = 0;
Array.Sort(capacity);
for (int i = 0; i < capacity.Length; ++i)
{
capacitySum += capacity[capacity.Length - 1 - i];
if (capacitySum >= appleSum)
return i + 1;
}
return capacity.Length;
}
}Was this solution helpful?
Related Problems
- 11. Container With Most Water(Medium)
- 122. Best Time to Buy and Sell Stock II(Medium)
- 135. Candy(Hard)
- 179. Largest Number(Medium)
- 321. Create Maximum Number(Unknown)
- 330. Patching Array(Hard)