Boats to Save People

medium

Minimum boats to carry people with weight limit

Boats to Save People

Key Insight

Sort people by weight. Pair lightest with heaviest if under limit, otherwise heaviest rides alone.

Step 1Sort Array
Sorted, limit = 3
left
1
0
2
1
2
2
right
3
3
converge

Sort people by weight. Limit = 3. Use two pointers at ends.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Boats to Save People

Sort people by weight. Pair lightest with heaviest if under limit, otherwise heaviest rides alone.

  1. Sort Array

    Sort people by weight. Limit = 3. Use two pointers at ends.

  2. Heaviest Alone

    left=0 (1) + right=3 (3) = 4 > 3. Heaviest rides alone. boats=1.

  3. Pair Found

    left=0 (1) + right=2 (2) = 3 ≤ 3. Pair them! boats=2.

  4. One Person Left

    left=1, right=1. One person remaining. boats=3.

  5. Result

    3 boats needed to save all 4 people.