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:

  1. Load boxes from storage (respecting capacity limits)
  2. Visit ports in order of loaded boxes, delivering each box
  3. 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

example_1.py — Basic case
$ Input: boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3
Output: 4
💡 Note: Ship takes all boxes in one trip: storage→port1→port2→port1→storage. Total: 4 trips
example_2.py — Weight constraint
$ Input: boxes = [[1,2],[3,3],[2,1],[1,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6
Output: 6
💡 Note: First trip: boxes [1,2] and [3,3] (weight=5), trips: storage→1→3→storage = 3. Second trip: remaining boxes, trips: storage→2→1→2→storage = 3. Total: 6
example_3.py — Box constraint
$ Input: boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 2, maxWeight = 7
Output: 9
💡 Note: Must make multiple trips due to maxBoxes=2 constraint, requiring optimal grouping

Visualization

Tap to expand
Ship Delivery Optimization ProcessStoragePort 1Port 2Port 3Return to StorageDynamic Programming ApproachState: DP[i]Min trips to deliverboxes[i..n-1]TransitionTry all validshipment sizesCost CalcUnique ports + return tripOptimizationDeque for O(n)complexityKey Constraints to Consider📦 Max Boxes per trip⚖️ Max Weight per trip📋 Must deliver in order
Understanding the Visualization
1
Analyze constraints
Identify valid shipment boundaries based on weight/box limits
2
Calculate costs
Count unique ports visited plus return trip for each shipment
3
Optimize transitions
Use deque to maintain only promising previous states
4
Build solution
DP forward to find minimum total trips
Key Takeaway
🎯 Key Insight: Use monotonic deque to maintain optimal candidates while sliding window ensures only valid shipment boundaries are considered, achieving O(n) complexity.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each element is added and removed from deque at most once

n
2n
Linear Growth
Space Complexity
O(n)

DP array and deque storage, plus prefix sum arrays

n
2n
Linearithmic Space

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
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K 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