Capacity To Ship Packages Within D Days

Posted: 6 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

You are the owner of a Shipment company. You use conveyor belts to ship packages from one port to another. The packages must be shipped within 'D' days.

The weights of the packages are given in an array 'WEIGHTS'. The packages are loaded on the conveyor belts every day in the same order as they appear in the array. The loaded weights must not exceed the maximum weight capacity of the ship.

Find out the least weight capacity so that you can ship all the packages within 'D' days.

Input Format:
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.

The first line of each test case contains two integers, ‘N’ and ‘D’, denoting the size of the array ‘WEIGHTS’ and the maximum weight capacity of the ship.

The second line of each test case contains ‘N’ integers denoting the elements of the array ‘WEIGHTS’.
Output Format:
For each test case, return the least weight capacity so that you can ship all the packages within 'D' days.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
Constraints:
1<= T <= 10
1 <= N <= 50000
1 <= D <= 50000
1 <= WEIGHTS[i] <= 500

Time Limit: 1 sec
Approach 1

The idea is to find the range in which the weight capacity of the ship can lie and then iterate in the given range and find the smallest weight capacity such that we can ship all the packages within ‘D’ days. The minimum value of the ship capacity is the maximum weight in the given array 'WEIGHTS', and the maximum value of the ship capacity is the sum of all the weights of the array 'WEIGHTS'.

 

The steps are as follows:

  • Initialize 'MIN_SHIP_CAPACITY' = maximum value of the array 'WEIGHTS' and 'MAX_SHIP_CAPACITY' = sum of all the values of array 'WEIGHTS'.
  • Iterate from i = 'MIN_SHIP_CAPACITY' to 'MAX_SHIP_CAPACITY':
  • Check if the given value of i is taken as the least weight capacity then Will it be possible to ship all the packages within D days?
    • Define a function 'CHECK_VALIDITY' which takes three arguments ‘capacity’, 'WEIGHTS', and ‘D’, which denotes the current weight capacity for which we are checking, the given array 'WEIGHTS', and the given integer ‘D’, respectively.
    • Initialize two variables, ‘CURRENT_DAY’ equal to 1 and  'CURRENT_DAY_WEIGHT' to 0, which denotes the number of the day for which we are shipping the packages, and the total weight that can be carried on the Kth Day, respectively.
    • Iterate over the array 'WEIGHTS':
      • Check if, after adding the current element, the total weight for the current day exceeds the weight capacity of the ship or not.
      • If it doesn’t exceed, then add the current weight to CURRENT_DAYWeight.
      • If it exceeds, then increase the required number of days by incrementing the ‘CURRENT_DAY’ and then set the value of 'CURRENT_DAY_WEIGHT' to the current element.
  • Check if ‘CURRENT_DAY’ becomes greater than ‘D’. If it becomes greater than ‘D’, it means packages can’t be delivered in the expected number of days. Return false.
  • Return true as the packages can be delivered within ‘D’ days.
  • If 'CURRENT_VALIDITY' returns true, then return i as it is the least answer.
Try Problem