You have the task of delivering boxes from storage to their ports using only one ship. The ship has limits on the number of boxes and total weight it can carry.

You are given an array boxes where boxes[i] = [ports​​i​, weighti], and three integers portsCount, maxBoxes, and maxWeight.

  • ports​​i is the port where you need to deliver the ith box
  • weightsi is the weight of the ith box
  • portsCount is the number of ports
  • maxBoxes and maxWeight are the ship's capacity limits

The boxes must be delivered in the given order. The ship follows these steps:

  1. Take boxes from the queue without violating maxBoxes and maxWeight constraints
  2. For each loaded box in order, make a trip to deliver it (if already at the correct port, no trip needed)
  3. Return to storage to take more boxes

The ship must end at storage after all deliveries. Return the minimum number of trips needed.

Input & Output

Example 1 — Basic Case
$ Input: boxes = [[1,1],[4,6],[1,2],[2,7],[3,2]], portsCount = 3, maxBoxes = 4, maxWeight = 6
Output: 6
💡 Note: Trip 1: Take boxes [1,1],[4,6] but weight exceeds. Take only [1,1], deliver to port 1, return. Trip 2: Take [4,6], deliver to port 4, return. Trip 3: Take [1,2],[2,7] but weight exceeds. Take [1,2], deliver to port 1, return. Trip 4: Take [2,7], deliver to port 2, return. Trip 5: Take [3,2], deliver to port 3. Total: 6 trips.
Example 2 — Efficient Grouping
$ Input: boxes = [[1,2],[3,3],[1,1]], portsCount = 3, maxBoxes = 3, maxWeight = 6
Output: 4
💡 Note: Trip 1: Take all boxes [1,2],[3,3],[1,1] (weight = 6, boxes = 3). Deliver to port 1, then port 3 (port 1 again is same as current). Total: 2 trips since no return needed.
Example 3 — Single Box Per Trip
$ Input: boxes = [[1,4],[2,5]], portsCount = 2, maxBoxes = 1, maxWeight = 10
Output: 4
💡 Note: Trip 1: Take [1,4], deliver to port 1, return. Trip 2: Take [2,5], deliver to port 2. Total: 4 trips.

Constraints

  • 1 ≤ boxes.length ≤ 105
  • 1 ≤ portsCount, boxes[i][0] ≤ 105
  • 1 ≤ boxes[i][1] ≤ 105
  • 1 ≤ maxBoxes, maxWeight ≤ 105

Visualization

Tap to expand
Delivering Boxes from Storage to Ports INPUT boxes array: port:1 wt:1 port:4 wt:6 port:1 wt:2 port:2 wt:7 port:3 wt:2 Parameters: portsCount = 3 maxBoxes = 4 maxWeight = 6 boxes = [[1,1],[4,6], [1,2],[2,7],[3,2]] ALGORITHM (DP) 1 Define DP State dp[i] = min trips for boxes[0..i-1] 2 Sliding Window Track valid box range under constraints 3 Count Port Changes Track port switches in current segment 4 Update DP dp[i] = min(dp[j] + portChanges + 2) DP Transitions: i=0 i=1 i=2 i=3 i=4 i=5 0 2 4 4 6 6 FINAL RESULT Trip Breakdown: Trip 1: Box[0] to Port 1 Trip 2: Box[1] to Port 4 Trip 3: Box[2] to Port 1 Trip 4: Box[3] to Port 2 Trip 5: Box[4] to Port 3 Trip 6: Return to Storage OUTPUT 6 Minimum trips needed Key Insight: Use Dynamic Programming with a sliding window to efficiently find the minimum trips. The DP state dp[i] represents min trips to deliver boxes[0..i-1]. For each position, we extend the window while respecting maxBoxes and maxWeight constraints, counting port changes to minimize total trips (port visits + returns). TutorialsPoint - Delivering Boxes from Storage to Ports | Dynamic Programming Approach
Asked in
Google 15 Amazon 25 Microsoft 12
23.0K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen