Find the Smallest Divisor Given a Threshold
LeetCode 1283 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
Monotonic total ⇒ binary search on d.
मैंने 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
Search d in [1, max(nums)].
- 2
total(d) = Σ ceil(num / d).
- 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.