1760. Minimum Limit of Balls in a Bag - cocoder39/coco39_LC GitHub Wiki

1760. Minimum Limit of Balls in a Bag

Didn't come up with a solution during context.

Thinking process:

  1. Given a target penalty, what's the min number of operations to take?
  2. the lower the penalty, the more operations will be needed -> binary search
class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        def isValid(k):
            count = 0
            for num in nums:
                count += math.ceil(num / k) - 1
                if count > maxOperations:
                    return False
            return True
                    
        
        low, high = 1, max(nums)
        
        while low + 1 < high:
            mid = low + (high - low) // 2
            if isValid(mid):
                high = mid
            else:
                low = mid
        
        if isValid(low):
            return low
        return high