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.