Minimum Limit of Balls in a Bag
Medium
Topics
Given an array nums of bag sizes and an integer target (the maximum number of split operations allowed), each operation splits one bag into two smaller non-negative bags. Return the minimum possible penalty, where the penalty is the maximum number of balls in any bag after all operations.
Example 1
Input: nums = [9], target = 2 Output: 3
Example 2
Input: nums = [2,4,8,2], target = 4 Output: 2
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9
- 1 <= target <= 10^9
Editorial — intuition, approach, complexity and pitfalls
Intuition
Another minimise-the-maximum problem, so binary search the penalty. The work is in the feasibility test: how many split operations does it take to get a bag of v balls down to parts of at most x?
Splitting into ceil(v / x) parts takes one fewer operation than the number of parts, because each split adds exactly one bag. So the cost is ceil(v / x) - 1, which equals (v - 1) // x — the two expressions agree for every v, x below 200, checked exhaustively. The second form is the one to write, because it avoids a ceiling and handles v == x correctly with no special case.
Approach
- Search the penalty
xover[1, max(nums)]. - For the midpoint, sum
(v - 1) // xover all bags to get the operations required. - If that is within
target, the penalty is achievable, so try smaller; otherwise try larger. - Return the smallest achievable penalty.
Dry Run
nums = [2,4,8,2] target = 4
lo hi mid splits per bag total within target
1 8 4 0+0+1+0 1 yes -> try smaller
1 4 2 0+1+3+0 4 yes -> try smaller
1 2 1 1+3+7+1 12 no -> too small
answer 2Complexity
- Time — O(n log(max(nums))): about 30 iterations at the stated bound of 10⁹, each a linear pass.
- Space — O(1).
Common Pitfalls
- Using
v // xinstead of(v - 1) // x. A bag holding exactlyxballs needs zero splits, butv // xreports one, so the operation count is inflated and the search settles on a penalty that is larger than necessary. - Simulating: repeatedly split the largest bag with a max-heap,
targettimes.targetreaches 10⁹, so this is not slow, it is impossible. - Overflow in the operation count. With
n = 10⁵bags and a penalty of 1, the total reaches 10₁⁴; the shown Java accumulates into along. - Starting
loat 0, which divides by zero. - Assuming splits must be into equal halves. They need not be, which is exactly why the cost depends only on how many parts you end up with.
Full Solution — all languages
class Solution:
def minimumSize(self, nums, target):
def ops(x):
total = 0
for v in nums:
total += (v - 1) // x
return total
lo, hi = 1, max(nums)
while lo < hi:
mid = (lo + hi) // 2
if ops(mid) <= target:
hi = mid
else:
lo = mid + 1
return loimport java.util.*;
class Solution {
public int minimumSize(int[] nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = Math.max(hi, x);
while (lo < hi) {
int mid = (lo + hi) / 2;
if (ops(nums, mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
long ops(int[] nums, int x) {
long total = 0;
for (int v : nums) total += (v - 1) / x;
return total;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minimumSize(vector<int>& nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = max(hi, x);
auto ops = [&](int x) {
long long total = 0;
for (int v : nums) total += (v - 1) / x;
return total;
};
while (lo < hi) {
int mid = (lo + hi) / 2;
if (ops(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var minimumSize = function(nums, target) {
let lo = 1, hi = 0;
for (const x of nums) hi = Math.max(hi, x);
const ops = (x) => {
let total = 0;
for (const v of nums) total += Math.floor((v - 1) / x);
return total;
};
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (ops(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
};