Leetcode 1011: Capacity To Ship Packages Within D Days

grid47
grid47
Exploring patterns and algorithms
Jul 28, 2024 8 min read

You are tasked with shipping packages using a ship with a limited weight capacity. Each package has a given weight and must be shipped in order within a specified number of days. Your objective is to determine the minimum weight capacity of the ship that allows all the packages to be shipped within the given days, without exceeding the ship’s weight capacity on any single day.
Problem
Approach
Steps
Complexity
Input: The input consists of two components: an array of integers representing the weights of the packages, and an integer representing the number of days available to ship all the packages.
Example: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
Constraints:
• 1 <= days <= weights.length <= 5 * 10^4
• 1 <= weights[i] <= 500
Output: The output should be a single integer representing the minimum ship capacity required to ship all packages within the given days.
Example: Output: 15
Constraints:
• The result should be an integer representing the minimum weight capacity.
Goal: The goal is to find the least weight capacity of the ship that ensures all the packages are shipped within the given days without exceeding the capacity each day.
Steps:
• Use binary search to determine the minimum ship capacity.
• Start with a lower bound of the heaviest package weight and an upper bound that is the sum of all package weights.
• For each mid value (representing a ship's weight capacity), simulate shipping the packages within the given days and check if it's feasible to ship them all without exceeding the capacity each day.
Goal: Ensure the solution handles the input size efficiently and produces the correct result within the given constraints.
Steps:
• The number of packages can be up to 50,000, so the solution must be optimized to handle large inputs.
• The weight of each package is between 1 and 500.
Assumptions:
• The packages must be shipped in the given order, meaning they cannot be rearranged.
Input: Input: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
Explanation: In this case, the minimum ship capacity needed is 15, as the packages can be distributed across the days as follows: (1, 2, 3, 4, 5) on day 1, (6, 7) on day 2, (8) on day 3, (9) on day 4, and (10) on day 5. The total weight each day does not exceed 15, and all packages are shipped within 5 days.

Input: Input: weights = [3, 2, 2, 4, 1, 4], days = 3
Explanation: For this input, a ship capacity of 6 is enough to ship all the packages within 3 days. The packages can be shipped as follows: (3, 2) on day 1, (2, 4) on day 2, and (1, 4) on day 3.

Input: Input: weights = [1, 2, 3, 1, 1], days = 4
Explanation: For this case, the minimum ship capacity is 3, which is sufficient to ship the packages within 4 days as follows: (1) on day 1, (2) on day 2, (3) on day 3, and (1, 1) on day 4.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus