⚡Flashcard Drill➔
NeetCode #282LC-1760MediumBinary Search
← Back to All Problems

#282 · #1760 · Minimum Limit of Balls in a Bag(袋子里最少数目的球)

📌 Problem Statement & Constraints

You are given an integer array nums where nums[i] is the number of balls in the i-th bag, and an integer maxOperations. Each operation splits one bag into two bags with positive counts. Return the minimum possible maximum bag size after at most maxOperations operations. Constraints: 1 <= nums.length <= 10^5, 1 <= maxOperations, nums[i] <= 10^9.

💡 Core Algorithmic Approaches

  1. Binary search on the maximum bag size x: "can every bag be reduced to at most x with at most maxOperations splits" is monotone.
  2. A bag with b balls needs ceil(b / x) - 1 splits, which simplifies to (b - 1) // x.
  3. Sum the splits and compare with maxOperations.
  4. The lower bound is 1 and the upper bound is max(nums).

💻 Benchmark Python3 Implementation

class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        lo, hi = 1, max(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            # splits needed to bring every bag down to at most mid
            ops = sum((b - 1) // mid for b in nums)
            if ops <= maxOperations:
                hi = mid               # feasible -> try a smaller cap
            else:
                lo = mid + 1
        return lo

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n log(max(nums))): each feasibility check is O(n).
💾 Space Complexity
O(1): a few scalars.

⚠️ Interview Pitfalls & Follow-ups

  • Using b // mid instead of (b - 1) // mid: the number of splits is ceil(b / mid) - 1, which equals (b - 1) // mid -- the -1 is essential, otherwise a bag that already fits is counted as needing a split.
  • Using ceil(b / mid): that counts pieces, not splits; the split count is one less.
  • Using hi = mid - 1 when feasible: could skip the optimum.
  • Assuming maxOperations must be used exactly: at most, so extra operations are simply unused.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Binary Search Problems

中文速览与面试要点

袋子里最少数目的球

给你一个整数数组 nums,其中 nums[i] 表示第 i 个袋子里球的数目。同时给你一个整数 maxOperations。你可以进行如下操作至多 maxOperations 次:选择任意一个袋子,并将袋子里的球分到 2 个新的袋子中,每个袋子里都有 正整数 个球。比方说,一个袋子里有 5 个球,你可以把它们分到两个新袋子里,分别有 1 个和 4 个球,或者分别有 2 个和 3 个球。你的开销是单个袋子里球数目的 最大值 ,你想要 最小化 开销。请你返回进行上述操作后的最小开销。约束:1 ≤ nums.length ≤ 10^5;1 ≤ maxOperations, nums[i] ≤ 10^9。

核心要点

  • 二分答案(标准解) :二分「单个袋子的球数上限 x」,判定「能否在 maxOperations 次操作内使所有袋子球数 ≤ x」。
  • 操作数公式 :把一袋 x 个球分成每袋不超过 limit,需要 ceil(x / limit) − 1 次操作。整数写法为 (x − 1) // limit 。
  • 单调性 :limit 越大,所需操作数越少,因此可以二分(求最小值)。
  • 答案范围 : [1, max(nums)] 。

常见陷阱

  • 操作数公式写成 x // limit :会多算一次。正确是 ceil(x/limit) − 1 = (x − 1) // limit 。例如 x = 5、limit = 2,需要分成 [2,2,1] 共 2 次操作, (5−1)//2 = 2 ✓,而 5//2 = 2 恰好也对,但 x = 4、limit = 2 时需 1 次操作, (4−1)//2 = 1 ✓ 而 4//2 = 2 ✗。
  • 用 math.ceil(x / limit) 而不减 1 : ceil 是「分成几份」,操作次数是份数减 1。
  • 下界写成 0 :会除零。
  • 上界写成 sum(nums) :虽然正确但二分次数更多。

参考实现

class Solution:
    def minimumSize(self, nums: list[int], maxOperations: int) -> int:
        def ops_needed(limit: int) -> int:
            # 每袋分成不超过 limit,需要 (x-1)//limit 次操作
            return sum((x - 1) // limit for x in nums)

        l, r = 1, max(nums)
        while l < r:
            mid = (l + r) // 2
            if ops_needed(mid) <= maxOperations:
                r = mid                 # 可行 -> 尝试更小的上限
            else:
                l = mid + 1
        return l