Minimum Limit of Balls in a Bag
Minimum Limit of Balls in a Bag
Pattern: Binary search on answer
Idea:
Variations :
π» Code
def minimumSize(nums, maxOperations):
low = 1
high = max(nums)
def feasible(limit):
operations = 0
for balls in nums:
operations += (balls - 1) // limit
if operations > maxOperations:
return False
return True
while low < high:
mid = low + (high - low) // 2
if feasible(mid):
high = mid
else:
low = mid + 1
return low
Time complexity - O(n log M ) , M = max(nums) Aux. Space complexity - O(1)
LeetCode 1760 β Binary Search on Answer + Counting
You have bags containing different numbers of balls. In one operation, you can split a bag into two non-empty bags.
Find the minimum possible maximum number of balls in any bag after performing at most maxOperations splits.
Key Idea
We want to:
Minimize the maximum number of balls in a bag.
Guess a candidate maximum size limit and ask:
How many split operations are required to make every bag contain at most
limitballs?
For a bag containing x balls:
Equivalent integer form:
So:
limit is feasible when:
Why ceil(x / limit) - 1?
Suppose a bag has 9 balls and:
limit = 3
We need:
[3] [3] [3]
Thatβs 3 bags, requiring:
3 - 1 = 2 splits
Therefore:
For:
x = 10, limit = 3
we need:
[3] [3] [4]
3 bags β 2 splits:
Why Binary Search Works
As limit increases, fewer splits are necessary.
limit:
1 2 3 4 5 6 ...
operations:
β
many -----------β fewer
feasible:
F F F T T T ...
β
first feasible
Therefore:
Smaller limit β harder β more operations
Larger limit β easier β fewer operations
So we search for the first feasible limit.
Search Space
Lower bound
low = 1
Every bag must contain at least one ball.
Upper bound
high = max(nums)
With no splits, the largest bag already gives a valid upper bound.
Therefore:
[1, max(nums)]
Approach
For each candidate limit:
-
Calculate how many operations each bag needs.
-
Stop early if operations exceed
maxOperations. -
If total operations are within the limit, the candidate is feasible.
-
Binary-search the smallest feasible
limit.
Python Solution β Implicit Answer Style
def minimumSize(nums, maxOperations):
low = 1
high = max(nums)
def feasible(limit):
operations = 0
for balls in nums:
operations += (balls - 1) // limit
if operations > maxOperations:
return False
return True
while low < high:
mid = low + (high - low) // 2
if feasible(mid):
high = mid
else:
low = mid + 1
return low
Why (balls - 1) // limit?
It is an integer-only way of computing:
This is a particularly useful formula to remember.
Explicit Answer Style
def minimumSize(nums, maxOperations):
low = 1
high = max(nums)
ans = high
def feasible(limit):
operations = 0
for balls in nums:
operations += (balls - 1) // limit
if operations > maxOperations:
return False
return True
while low <= high:
mid = low + (high - low) // 2
if feasible(mid):
ans = mid
high = mid - 1
else:
low = mid + 1
return ans
Both are equivalent.
For this problem, the implicit first-True version is particularly clean.
Dry Run
nums = [9]
maxOperations = 2
Try:
limit = 3
Required operations:
Feasible.
Try:
limit = 2
4 > 2 β not feasible.
Therefore:
answer = 3
The bag can become:
[3] [3] [3]
using exactly 2 operations.
Complexity
Let:
-
= number of bags
-
=
max(nums)
Each feasibility check:
Binary search:
Therefore:
Auxiliary Space
Important Quirks
1. Number of operations β number of resulting bags
If a bag needs k final bags:
This is why:
is used.
2. operations <= maxOperations
We donβt need to use all operations.
operations <= maxOperations
is sufficient.
3. Splitting can be done optimally without simulating it
You might initially think you need to actually perform the splits.
You donβt.
For a fixed limit, the mathematical formula directly tells us the minimum number of splits required.
This makes the validator .
Connection to Previous Problems
This is closely related to the two problems youβve just studied:
Smallest Divisor
Minimized Maximum Products
Balls in a Bag
All three follow:
Candidate X
β
Calculate required resource
β
Required <= available?
β
FFFFTTTT
β
First True
The key difference is what the validator counts.
Important Variations
β Same family
-
Smallest Divisor Given a Threshold β quotient/count calculation
-
Minimized Maximum of Products β containers required
-
Capacity to Ship Packages β greedy grouping
-
Split Array Largest Sum β greedy partition
The reusable pattern
Minimize the maximum β guess the maximum β calculate the resources needed β check whether resources fit β first feasible.
Common Mistakes
-
β Using
balls // limitas the number of operations. -
β Forgetting the
-1inceil(balls / limit) - 1. -
β Simulating every split.
-
β Using
operations == maxOperations. -
β Searching for the exact original bag sizes instead of
[1, max(nums)]. -
β Forgetting that the predicate is first True.
Pattern Recognition
When you see:
βMinimize the maximum size after performing at most K operations, where each operation splits/reduces something.β
Think:
MINIMIZE THE MAXIMUM
β
Guess maximum allowed size X
β
How many operations are minimally required?
β
operations <= K ?
β
FFFFTTTT
β
FIRST TRUE
Mental hook
βDonβt simulate the splits. For a candidate maximum
X, calculate how many splits are mathematically necessary.β
This is an important evolution of the Minimize the Maximum pattern: the validator doesnβt always need greedy simulationβit can sometimes be reduced to a direct mathematical counting formula.