Delivering Boxes from Storage to Ports
You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a limit on the number of boxes and the total weight that it can carry.
You are given an array boxes, where boxes[i] = [ports_i, weight_i], and three integers portsCount, maxBoxes, and maxWeight.
ports_iis the port where you need to deliver thei^thbox andweight_iis the weight of thei^thbox.portsCountis the number of ports.maxBoxesandmaxWeightare the respective box and weight limits of the ship.
The boxes need to be delivered in the order they are given. The ship will follow these steps:
- The ship will take some number of boxes from the
boxesqueue, not violating themaxBoxesandmaxWeightconstraints. - For each loaded box in order, the ship will make a trip to the port the box needs to be delivered to and deliver it. If the ship is already at the correct port, no trip is needed, and the box can immediately be delivered.
- The ship then makes a return trip to storage to take more boxes from the queue.
The ship must end at storage after all the boxes have been delivered.
Return the minimum number of trips the ship needs to make to deliver all boxes to their respective ports.
Example 1
Input
boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3Output
4The optimal strategy is to take all boxes, visit port 1, then port 2, then port 1 again, and return to storage for 4 trips.
Example 2
Input
boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6Output
6The optimal strategy uses three shipments costing 2 trips each: the first box, then the second through fourth boxes, then the fifth box, for a total of 6 trips.
Constraints
- 1 <= boxes.length <= 10^5
- 1 <= portsCount, maxBoxes, maxWeight <= 10^5
- 1 <= ports_i <= portsCount
- 1 <= weights_i <= maxWeight