Boats to Save People (LeetCode 881): Solution and Explanation

Medium
Array
Two Pointers
Greedy
Sorting

Each person has a weight, and each boat carries at most two people whose total weight is at most limit. Return the fewest boats that carry everyone.

Intuition

Look at the heaviest person first. They have the fewest possible boat partners, so they are the hardest to place.

If anybody can share a boat with the heaviest person, the lightest person can, because the lightest is no heavier than anyone else. So the only question for the heaviest person is whether the lightest fits.

If the lightest fits, pairing them is never a mistake: whoever the lightest would otherwise ride with could swap places without breaking any weight limit. If the lightest does not fit, nobody fits, so the heaviest rides alone.

After sorting, two pointers track the lightest and heaviest people still on the dock. Every step launches exactly one boat and removes the heaviest person, plus the lightest when they fit.

Approach: Sort and two pointers

  1. Sort people in ascending order.
  2. Set light = 0, heavy = len(people) - 1 and boats = 0.
  3. While light <= heavy: if people[light] + people[heavy] <= limit, the lightest boards too, so light += 1.
  4. In every iteration the heaviest boards (heavy -= 1) and one boat leaves (boats += 1).
  5. Return boats.

Step-by-step example

people = [3,2,2,1], limit = 3
  1. Sorted: [1, 2, 2, 3]. light = 0 (weight 1), heavy = 3 (weight 3).
  2. 1 + 3 = 4 > 3, so 3 rides alone. boats = 1, heavy = 2.
  3. 1 + 2 = 3 <= 3, so 1 and 2 share a boat. boats = 2, light = 1, heavy = 1.
  4. light == heavy: the last person (2) rides alone. boats = 3.
  5. light > heavy, so everyone is off the dock. Return 3.

Code: Python, Java, C++

1class Solution:
2    def numRescueBoats(self, people: List[int], limit: int) -> int:
3        people.sort()
4        boats = 0
5        light, heavy = 0, len(people) - 1
6        while light <= heavy:
7            if people[light] + people[heavy] <= limit:
8                light += 1
9            heavy -= 1
10            boats += 1
11        return boats
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n)

Sorting dominates. The two-pointer pass is O(n) because each step moves heavy one place left.

Space

O(1)

The weights are sorted in place and only a few counters are used, aside from the sort's own memory.

Other approaches

Counting sort on weights (time O(n + limit), space O(limit))

Since weights are at most limit (up to 30,000), bucket the weights instead of comparison sorting, then run the same two-pointer pairing over the buckets.

Pair each person with the heaviest partner that fits (time O(n log n), space O(n))

For each heaviest person, search a sorted multiset for the heaviest partner that still fits. Also optimal, but it needs a balanced tree and gains nothing over pairing with the lightest.

Common mistakes

  • Using light < heavy as the loop condition. When one person is left in the middle, they still need a boat.
  • Trying to fit three people in a boat. The two-person cap is a hard rule even if the weights would allow more.
  • Moving the light pointer when the pair does not fit. Only the heaviest leaves in that case.
  • Pairing neighbors after sorting (two lightest together). That can strand heavy people who could have shared with a light one.

Frequently asked questions

Why pair with the lightest person rather than the heaviest one that fits?

The lightest person is the partner most likely to fit, and choosing them never costs a boat. If some best plan seats the lightest person with someone else, you can swap that person with the heaviest person's partner (or put them alone) without breaking any limit, so a plan with the greedy pairing is just as good.

Can a person ever be too heavy for any boat?

No. The constraints guarantee every weight is at most limit, so anyone can ride alone.

Why is the answer at least ceil(n / 2)?

Each boat carries at most two people, so n people need at least ceil(n / 2) boats. The greedy reaches that bound whenever enough pairs fit.