Boats to Save People

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Boats to Save People

Approach

Sort people by weight. Use two pointers: pair the lightest person with the heaviest. If they fit together within the limit, both board one boat and both pointers move inward. Otherwise, the heaviest person takes a boat alone and only the right pointer moves.

Complexity Analysis

Time
O(n log n)
Space
O(1)

Pattern

Two Pointers (Converging) + Greedy

Why It Works

Greedy pairing of lightest with heaviest maximizes space utilization. If the lightest cannot pair with the heaviest, no one else can pair with that heavy person either.

Updated Feb 2026