Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1283 · Binary Search on Answer

Find the Smallest Divisor Given a Threshold

LeetCode 1283 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Monotonic total ⇒ binary search on d.

நடுத்தரம்ArrayBinary Search
LeetCode இல் திற ↗

நான் Find the Smallest Divisor Given a Threshold (LeetCode 1283) ஐ இப்படித் தீர்த்தேன் — Binary Search on Answer பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

A bigger divisor makes every ceil(num / d) smaller, so the total only goes down as d grows. Binary search the smallest divisor whose total is within the threshold.

Approach

  1. 1

    Search d in [1, max(nums)].

  2. 2

    total(d) = Σ ceil(num / d).

  3. 3

    Within threshold → try smaller d; else larger.

நான் எப்படித் தீர்த்தேன்

Binary search on divisorPythonO(n log M) time
class Solution:
    def smallestDivisor(self, nums: List[int], threshold: int) -> int:
        def isTotalSumWithinThreshold(mid):
            totalSum=0
            for num in nums:
                totalSum+=math.ceil(num/mid)
            if totalSum<=threshold:
                return True
            else:
                False
        low =1
        high = max(nums)
        while low<=high:
            mid = (low+high)//2
            withinThreshold = isTotalSumWithinThreshold(mid)
            if withinThreshold:
                high = mid-1
            else:
                low=mid+1
        return low
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Find the Smallest Divisor Given a Threshold — அடிக்கடி கேட்கப்படும் கேள்விகள்

Find the Smallest Divisor Given a Threshold பின்னாலுள்ள intuition என்ன?

A bigger divisor makes every ceil(num / d) smaller, so the total only goes down as d grows. Binary search the smallest divisor whose total is within the threshold.

Find the Smallest Divisor Given a Threshold (LeetCode 1283) ஐ எப்படித் தீர்ப்பது?

1. Search d in [1, max(nums)]. 2. total(d) = Σ ceil(num / d). 3. Within threshold → try smaller d; else larger.

Find the Smallest Divisor Given a Threshold இன் time மற்றும் space complexity என்ன?

Time: O(n log M). Space: O(1). (Accepted approach: Binary search on divisor.)

Find the Smallest Divisor Given a Threshold இல் பொதுவான தவறுகள் என்ன?

The helper falls through to None when the total is too big — None is falsy, so it still works, but return False explicitly.