Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Capacity To Ship Packages Within D Days

Medium Acceptance 65% Points 30

You are given an array weights[] where weights[i] represents the weight of the ith package, and an integer days representing the number of days within which all packages must be shipped. Packages must be shipped in the given order, and each day you can ship packages whose total weight does not exceed the ship’s capacity. Return the minimum ship capacity required to ship all packages within the given number of days.

Examples
Example 1

Example 1

Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5

Output: 15

Explanation: One possible way is: Day 1 ? [1,2,3,4,5] ? total weight = 15 Day 2 ? [6,7] ? total weight = 13 Day 3 ? [8] ? total weight = 8 Day 4 ? [9] ? total weight = 9 Day 5 ? [10] ? total weight = 10 The minimum capacity required is 15.

Example 2

Example 2

Input: weights = [3,2,2,4,1,4], days = 3

Output: 6

Explanation: One possible way is: Day 1 ? [3,2] ? total weight = 5 Day 2 ? [2,4] ? total weight = 6 Day 3 ? [1,4] ? total weight = 5 The minimum capacity required is 6.

Example 3

Example 3

Input: weights = [1,2,3,1,1], days = 4

Output: 3

Explanation: One possible way is: Day 1 ? [1,2] ? total weight = 3 Day 2 ? [3] ? total weight = 3 Day 3 ? [1] ? total weight = 1 Day 4 ? [1] ? total weight = 1 The minimum capacity required is 3.

Constraints
  • 1 = weights.length = 5 × 104
  • 1 = weights[i] = 500
  • 1 = days = weights.length
Topics
Binary Search
Companies
Amazon Microsoft Apple Visa
Added 19 Jul 2026 65% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3 4 5 6 7 8 9 10
5
15