1283. Find the Smallest Divisor Given a Threshold

1283. Find the Smallest Divisor Given a Threshold

這題只要先做過 875. Koko Eating Bananas 就會了。

class Solution:
    def smallestDivisor(self, nums: List[int], threshold: int) -> int:
        
        def findDivisor(divisor):
            res = 0
            for num in nums:
                res += num//divisor
                if num % divisor != 0:
                    res += 1
            return res
        
        left = 1
        right = max(nums)
        
        while left <= right:
            mid = left + (right - left)//2
            divisor = findDivisor(mid)
            if divisor <= threshold: # divisor is too large
                right = mid - 1
            else:
                left = mid + 1
        
        
        return left