Solution For Capacity To Ship Packages Within D Days

Problem Statement:

You have a list of n integers costs where costs[i] is the cost of ith item. You want to ship all of the items to someplace within d days.

You have to ship the packages in such a way that you minimize the total cost of shipping all the packages. You are allowed to split the packages between days. If you split the packages between days, the cost of shipping on each day cannot exceed some maximum cost C.

Write a function to determine the minimum possible total cost of shipping all the given packages within d days.

Solution:

We need to find the minimum value of C such that we can ship all the packages within d days. We can apply binary search to find this value. Here is a detailed algorithm:

• Set the lower bound of the binary search to the maximum element in the costs array. This is because we cannot ship any package with a cost higher than this value on any day.
• Set the upper bound of the binary search to the sum of all elements in the costs array. This is because we can ship all the packages in a single day if the maximum cost is equal to the sum of all package costs.
• While the lower bound is less than or equal to the upper bound, do the following:
• Calculate the mid value of the lower and upper bound.
• Check if it is possible to ship all the packages using the mid value as the maximum cost per day. We can do this by simulating d days of shipping using greedy approach.
• If it is possible to ship all the packages, update the upper bound to mid – 1.
• Otherwise, update the lower bound to mid + 1.
• Return the lower bound as the final answer.

Here is the code implementation for the algorithm in Python:

“`
def shipWithinDays(costs, D):
# Set lower and upper bounds of binary search
lo, hi = max(costs), sum(costs)

``````# Perform binary search
while lo <= hi:
mid = (lo + hi) // 2
cur, nxt = 0, 1
days = 1
while nxt <= len(costs) and days <= D:
# Try to add next package to current day
if sum(costs[cur:nxt]) <= mid:
nxt += 1
# Move to next day
else:
cur = nxt
nxt = cur + 1
days += 1

# Check if all packages were shipped within D days
if days <= D:
hi = mid - 1
else:
lo = mid + 1

return lo
``````

“`

The time complexity of the algorithm is O(n log m) where n is the number of packages and m is the range of possible values for the maximum cost per day. The space complexity is O(1) as we are only using constant amount of extra memory.

Step by Step Implementation For Capacity To Ship Packages Within D Days

```class Solution {
public int shipWithinDays(int[] weights, int D) {
int left = 0, right = 0;
for (int w: weights) {
left = Math.max(left, w);
right += w;
}
while (left < right) {
int mid = (left + right) / 2, need = 1, cur = 0;
for (int w: weights) {
if (cur + w > mid) {
need += 1;
cur = 0;
}
cur += w;
}
if (need > D) left = mid + 1;
else right = mid;
}
return left;
}
}```
```def shipWithinDays(weights, D):
left = max(weights)
right = sum(weights)
while left < right:
mid = (left + right) // 2
cur, need = 0, 1
for w in weights:
if cur + w > mid:
need += 1
cur = 0
cur += w
if need > D:
left = mid + 1
else:
right = mid
return left```
```var shipWithinDays = function(weights, D) {

//We need to find the minimum capacity that is required to ship all the packages within D days.

//The idea is to use binary search to find the answer. We will have two variables left and right which will represent the lower and upper bounds of our search space.

//We will iterate through the weights array and keep track of the current weight and the number of days required to ship that weight.

//If the current weight is greater than the capacity, we will increment the number of days required. Otherwise, we will add the current weight to the capacity.

//If the number of days required is greater than D, we will know that our capacity is too low and we will update the left bound to be the current capacity.

//Otherwise, we will update the right bound to be the current capacity.

//We will continue this process until left is equal to right, at which point we will return left as the minimum capacity required.

let left = 0;
let right = 0;

for(let weight of weights){
right += weight;
}

while(left < right){
let days = 1;
let capacity = left + Math.floor((right - left)/2);
let currentWeight = 0;

for(let weight of weights){
if(currentWeight + weight > capacity){
days++;
currentWeight = weight;
} else {
currentWeight += weight;
}
}

if(days > D){
left = capacity + 1;
} else {
right = capacity;
}
}

return left;

};```
```class Solution {
public:
int shipWithinDays(vector& weights, int D) {
int left = *max_element(weights.begin(), weights.end());
int right = accumulate(weights.begin(), weights.end(), 0);
while (left < right) {
int mid = left + (right - left) / 2, need = 1, cur = 0;
for (int w : weights) {
if (cur + w > mid) {
++need;
cur = 0;
}
cur += w;
}
if (need > D) left = mid + 1;
else right = mid;
}
return left;
}
};```
```using System;

public class Solution {

public int ShipWithinDays(int[] weights, int D) {

// check for edge cases
if (weights == null || weights.Length == 0) {
return 0; }

int left = 0, right = 0;

// get the sum of all the weights and the max weight
foreach (int weight in weights) {
right += weight;
left = Math.Max(left, weight);
}

// binary search for the capacity that satisfies the condition
while (left < right) {
int mid = left + (right - left) / 2;
if (CanShip(weights, D, mid)) {
right = mid;
}
else {
left = mid + 1;
}
}

return left;
}

private bool CanShip(int[] weights, int D, int capacity) {

int days = 1, curr = 0;

foreach (int weight in weights) {

// if adding the current weight would exceed the capacity,
// ship the current weights, reset the current weight to the new weight,
// and increment the number of days
if (curr + weight > capacity) {
days++;
curr = weight;

// if the number of days needed to ship the weights exceeds D,
// return false
if (days > D) {
return false;
}
}

// otherwise, add the weight to the current weight
else {
curr += weight;
}
}

return true;
}
}```

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]