Imagine you're managing a shipping company with one cargo ship that must deliver boxes from a central storage facility to various ports. Your ship has constraints: it can only carry a limited number of boxes and a maximum weight at any time.
You're given an array boxes where boxes[i] = [port_i, weight_i] representing the destination port and weight of each box. You also have three constraints: portsCount (total number of ports), maxBoxes (ship's box capacity), and maxWeight (ship's weight capacity).
The Challenge: Boxes must be delivered in the given order, and your ship follows this process:
- Load boxes from storage (respecting capacity limits)
- Visit ports in order of loaded boxes, delivering each box
- Return to storage for the next batch
Your goal is to find the minimum number of trips needed to deliver all boxes, where each trip is either going to a port or returning to storage.
Input & Output
Visualization
Time & Space Complexity
Each element is added and removed from deque at most once
DP array and deque storage, plus prefix sum arrays
Constraints
- 1 ≤ boxes.length ≤ 105
- 1 ≤ portsCount, boxes[i][0] ≤ 105
- 1 ≤ maxBoxes, boxes[i][1], maxWeight ≤ 109
- Boxes must be delivered in the given order