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

Capacity To Ship Packages Within D Days

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

Binary search capacity in [max(w), sum(w)] with a greedy day count.

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

நான் Capacity To Ship Packages Within D Days (LeetCode 1011) ஐ இப்படித் தீர்த்தேன் — Binary Search on Answer பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

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

Intuition

A larger ship capacity never needs more days, so binary search the capacity. The smallest possible is the heaviest package; the largest is everything in one day. Check each guess by loading packages greedily in order.

Approach

  1. 1

    low = max(weights), high = sum(weights).

  2. 2

    Greedy: start a new day when the next package would overflow.

  3. 3

    days ≤ D → shrink high; else raise low.

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

Binary search + greedy checkPythonO(n log S) time
class Solution:
    def shipWithinDays(self, weights: list[int], days: int) -> int:
        def is_feasibile(max_capacity):
            req_days = 1
            cur_taken= 0
            for weight in weights:
                if cur_taken+weight>max_capacity:
                    req_days+=1
                    cur_taken=weight
                else:
                    cur_taken+=weight
            return True if req_days<=days else False
        low=max(weights)
        high =sum(weights)
        while low<=high:
            mid = (low+high)//2
            if is_feasibile(mid):
                high = mid-1
            else:
                low = mid+1
        return low
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Capacity To Ship Packages Within D Days — அடிக்கடி கேட்கப்படும் கேள்விகள்

Capacity To Ship Packages Within D Days பின்னாலுள்ள intuition என்ன?

A larger ship capacity never needs more days, so binary search the capacity. The smallest possible is the heaviest package; the largest is everything in one day. Check each guess by loading packages greedily in order.

Capacity To Ship Packages Within D Days (LeetCode 1011) ஐ எப்படித் தீர்ப்பது?

1. low = max(weights), high = sum(weights). 2. Greedy: start a new day when the next package would overflow. 3. days ≤ D → shrink high; else raise low.

Capacity To Ship Packages Within D Days இன் time மற்றும் space complexity என்ன?

Time: O(n log S). Space: O(1). (Accepted approach: Binary search + greedy check.)

Capacity To Ship Packages Within D Days இல் பொதுவான தவறுகள் என்ன?

Packages must stay in order — no sorting.