Minimum Limit of Balls in a Bag

Medium

Topics
ArrayBinary Search

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

90
Input:  nums = [9], target = 2
Output: 3

Example 2

20418223
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

  1. Search the penalty x over [1, max(nums)].
  2. For the midpoint, sum (v - 1) // x over all bags to get the operations required.
  3. If that is within target, the penalty is achievable, so try smaller; otherwise try larger.
  4. 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 2

Complexity

  • 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 // x instead of (v - 1) // x. A bag holding exactly x balls needs zero splits, but v // x reports 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, target times. target reaches 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 a long.
  • Starting lo at 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 lo
Asked at Flipkart
SensAI
Beta
Tap the mic to stop voice mode