Skip to content

Latest commit

 

History

History

1283-FindtheSmallestDivisorGivenaThreshold

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Find the Smallest Divisor Given a Threshold

Problem can be found in here!

Solution: Binary Search

def smallestDivisor(nums: List[int], threshold: int) -> int:
    def count_division_sum(speed: int) -> int:
        times = 0
        for num in nums:
            counter, remainder = num // speed, num % speed
            if remainder:
                counter += 1
            times += counter

        return times

    start, end = 1, max(nums)
    while start < end:
        k = (start+end) // 2
        if count_division_sum(k) <= threshold:
            end = k
        else:
            start = k + 1

    return start

Time Complexity: O(logn), Space Complexity: O(1), where n is the length of array nums and m is the maximum number of bananas in piles.