↓Skip to main content
  1. LeetCode/

LeetCode 1011: Capacity To Ship Packages Within D Days

·2 mins· ·
LeetCode Medium Array Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-11-03

  • Submissions: Accepted (first try)

Approach
#

A larger capacity needs fewer days, which is monotone, so binary search on the answer (Binary Search Template 2):

  • The lower bound is max(weights): packages cannot be split, so a capacity below the heaviest one can never work.
  • The upper bound is sum(weights): everything ships in one day.
  • Check: load in order and start a new day when the next package does not fit, then compare the day count with days.

We want the smallest feasible capacity.

My Attempts
#

Accepted on the first try. Written the same day as LC 875 with the same closed-interval template: right = mid - 1 when feasible, otherwise left = mid + 1, and return left.

Solution
#

class Solution:
    def shipWithinDays(self, weights: List[int], days: int) -> int:
        def valid(cap):
            need, curr = 1, 0
            for w in weights:
                if curr + w <= cap:
                    curr += w
                else:                 # does not fit, start a new day
                    need += 1
                    curr = w
            return need <= days

        left, right = max(weights), sum(weights)
        while left <= right:
            mid = (left + right) // 2
            if valid(mid):
                right = mid - 1
            else:
                left = mid + 1
        return left

Complexity
#

Time O(n log S) where S is sum(weights); space O(1).

Mistakes to Avoid
#

  • A lower bound of 1: valid never checks whether a single package exceeds the capacity, so starting a new day counts as fitting. weights = [1, 5], days = 2 returns 1; the answer is 5. Starting at max(weights) makes that check unnecessary.
  • Count days from 1; the first day counts.

Interview Explanation
#

More capacity means fewer days, so I binary search on the answer. The lower bound is the heaviest package because packages cannot be split, and the upper bound is the total weight, which ships in one day. To check a capacity I load packages in order and start a new day when one does not fit, then compare with the limit. I look for the smallest feasible capacity. Time O(n log S), space O(1).

Related

LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies