Skip to contentKEEP THE CRAFTTHE SANCTUARY

Ship Packages Within D Days

Binary search the ship's capacity: find the smallest one that ships every package, in order, within the given days.

The problem

Implement Solution.shipWithinDays(weights, days). Packages must be shipped in the given order. Each day the ship takes the next few packages, as many as fit without going over its capacity, and a package cannot be split. Return the smallest whole-number capacity that ships all packages within days days. For example, weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and days = 5 returns 15. 1 <= days <= len(weights) <= 50000 and 1 <= weights[i] <= 500.

Starter code

class Solution:
    def shipWithinDays(self, weights, days):
        pass

Public examples

  1. weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5gives15
  2. weights = [2, 3, 5], days = 2gives5
  3. weights = [5, 1, 5, 1], days = 2gives6

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.