LeetCode 1283 Find the Smallest Divisor Given a Threshold (Python)

Posted by 小明MaxMing on November 6, 2020

题目

二分答案,左右边界为1和最大的数,注意这个除法是上取整

解题思路

Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold.

Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).

It is guaranteed that there will be an answer.

代码

class Solution:
    def smallestDivisor(self, nums: List[int], threshold: int) -> int:
        l, r = 1, max(nums)
        res = 0
        while l <= r:
            m = (l + r) // 2
            tot = sum((x - 1) // m + 1 for x in nums)
            if tot <= threshold:
                res = m
                r = m - 1
            else:
                l = m + 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅