DSA binary search learning path Level 2 This level · 7 of 7
Medium dsa binary-search

Capacity To Ship Packages Within D Days

The 30 second answer

Packages must be shipped in order, and I need the smallest ship capacity that lets me finish in at most days days. I binary-search the capacity between the heaviest package and the sum of all weights. For each mid capacity I count how many days a greedy left-to-right packing would take, and I search for the smallest capacity whose day count fits.

Could you say this in an interview?

Problem A conveyor has packages of given weights that must be shipped in order. A ship of capacity cap can carry any number of contiguous packages on one day, as long as their weights sum to at most cap. Return the smallest cap that ships every package in at most the given number of days. Original: LeetCode 1011: Capacity To Ship Packages Within D Days.

Understanding the prompt You cannot reorder packages. The minimum feasible capacity is at least the heaviest package, otherwise that package never fits, and at most the total weight, which ships everything in one day. Days needed is a decreasing function of capacity.

Pattern recognition Binary search on the answer, with a greedy feasibility check that packs left to right.

Brute force Try every capacity from max(weight) to sum(weight): O(n * sum).

Optimal lo = max(weight), hi = sum(weight). For a candidate cap, walk the array, starting a new day whenever adding the next package would exceed cap, and count the days. If that count is at most the budget, try a smaller cap (hi = mid), otherwise lo = mid + 1. The search lands on the minimum feasible cap.

Alternatives The same template as Koko Eating Bananas, with packing days in place of eating hours. A dynamic-programming split into exactly days parts is correct and much heavier; you do not need it.

Java solution

public class CapacityToShipPackages {
    static int shipWithinDays(int[] weights, int days) {
        int lo = 0, hi = 0;
        for (int w : weights) {
            lo = Math.max(lo, w);
            hi += w;
        }
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (daysNeeded(weights, mid) <= days) hi = mid;
            else lo = mid + 1;
        }
        return lo;
    }

    static int daysNeeded(int[] weights, int cap) {
        int days = 1, load = 0;
        for (int w : weights) {
            if (load + w > cap) {
                days++;
                load = 0;
            }
            load += w;
        }
        return days;
    }

    public static void main(String[] args) {
        System.out.println(shipWithinDays(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5)); // 15
        System.out.println(shipWithinDays(new int[] {3, 2, 2, 4, 1, 4}, 3));               // 6
        System.out.println(shipWithinDays(new int[] {1, 2, 3, 1, 1}, 4));                  // 3
    }
}

Complexity O(n log S) time, where S is the sum of the weights, and O(1) extra space.

Edge cases

  • Official samples: weights [1,2,3,4,5,6,7,8,9,10] with 5 days returns 15; [3,2,2,4,1,4] with 3 days returns 6; [1,2,3,1,1] with 4 days returns 3.
  • days = 1 returns the total weight; days = n returns the heaviest package.

Follow-ups

  • Split Array Largest Sum is the same search, this time minimising the largest of m contiguous parts.
  • Koko Eating Bananas is the hours-and-piles sibling.

Tests [1,2,3,4,5,6,7,8,9,10] with 5 days returns 15. [3,2,2,4,1,4] with 3 days returns 6. [1,2,3,1,1] with 4 days returns 3.

How to say it in the interview "I binary-search the ship capacity between the heaviest package and the total weight. For each mid I pack left to right and count the days I would need. I want the smallest capacity whose day count is still within the budget."

Sources (1)
  1. LeetCode 1011: Capacity To Ship Packages Within D Daysleetcode.com
Previous
Next