![]() |
VOOZH | about |
Given an array weights[] of n packages, the task is to find the least weight capacity of a boat to ship all Packages within d days. The weights[i] represent the weight of ith Package. Each day, we load the boat with packages without changing their order. We may not load more weight than the maximum weight capacity of the boat.
Example:
Input: weights[] = [1, 2, 3, 4, 5, 6, 7], d = 5
Output: 7
Explanation:
- Day 1: Load the boat with packages [1, 2, 3] (total weight = 6).
- Day 2: Load the boat with package [4] (total weight = 4).
- Day 3: Load the boat with package [5] (total weight = 5).
- Day 4: Load the boat with package [6] (total weight = 6).
- Day 5: Load the boat with package [7] (total weight = 7).
The minimum capacity of boat needed is 7. With this capacity, we can ship all packages within 5 days by optimally distributing the packages as shown above.
Input: weights[] = [5, 2, 7, 4, 1, 4], d = 3
Output: 9
Explanation:
- Day 1: Load the boat with packages [5, 2] (total weight = 7).
- Day 2: Load the boat with packages [7] (total weight = 7).
- Day 3: Load the boat with packages [4, 1, 4] (total weight = 9).
The minimum capacity needed is 9. We can ship all packages within 3 days by distributing the packages as shown above.
Table of Content
The very basic idea is to start with the minimum possible capacity of boat and gradually increase the capacity until we find a value that allows us to ship all packages within the given days.
, we iterate through the packages and try take cumulative sum of weights until we haven't exceeded our chosen capacity of boat. If the current day's weight exceeds the capacity, we start a new day. If all packages can be packed within the given days then this capacity is sufficient.
Code Implementation:
7
Time Complexity: O(n*sum), where n is the number of weights and 'sum' is the sum of all weights.
Auxiliary Space: O(1), As we are using constant extra space.
We can use binary search on the answer to solve because this problem have a monotonicrelationship: if a certain capacity C allows us to ship all packages within d days, any capacity greater than C will also work (and vice versa).
The minimum capacity must be at least the weight of the heaviest package (since the boat needs to carry every package individually at a minimum), and the maximum capacity can be the sum of all package weights (if we could ship all packages in one trip). We then perform a binary search within these bounds.
:
We simulate loading the packages onto the boat day by day, checking if the total number of days required is within the given limit. If the number of days exceeds the limit, we increase the lower bound of our search to look for a higher capacity. If it is within the limit, we decrease the upper bound to find if a lower capacity can still meet the requirement. This process continues until we find the minimum capacity that allows us to ship all packages within the given number of days.
Code Implementation:
7
Time complexity: O(n log(sum)), where n is the number of weights and 'sum' is the sum of all weights.
Auxiliary Space: O(1)