LeetCode #881 Medium

Boats to Save People

Boats to Save People is LeetCode 881 (Medium). people[i] is the weight of person i, and every boat can carry a total weight of at most limit. Return the fewest boats that carry everyone.

  • A boat holds at most two people, and their combined weight must be at most limit.
  • Nobody weighs more than limit, so everyone fits in a boat alone.

There can be 5 × 10⁴ people, so trying every way of pairing them is out of the question; sorting and one pass is the target.

Constraints
  • 1 <= people.length <= 5 * 10⁴
  • 1 <= people[i] <= limit <= 3 * 10⁴
two-pointersgreedysorting
Open on LeetCode ↗
02

Intuition

Look at the heaviest person still waiting. They need a boat whatever happens, so the only question is who rides with them. The best partner to try is the lightest person left: if even the lightest does not fit, nobody does, and the heaviest goes alone.

When the lightest does fit, pairing them is never a mistake. Take any best plan. If it sends the heaviest alone, move the lightest in with them. If it seats the heaviest with a partner, swap that partner with the lightest: the heaviest's boat still fits since the lightest weighs no more than the partner, and the partner's new boat fits since its other rider weighs no more than the heaviest. The count never grows.

How to spot this pattern

Groups of at most two under a weight or size cap, where the goal is the fewest groups. Sort, then match the largest remaining item with the smallest one.

03

Approach

Try it first

Sort [3, 2, 2, 1] with limit = 3 and seat people by hand, always starting from the heaviest. Count the boats, then note what you checked each time.

1

Sort the weights

After sorting, the lightest person still waiting is always at the left end and the heaviest at the right end, so each check takes constant time.

2

Point at both ends

Set left = 0, right = len(people) - 1 and boats = 0. Everyone from left to right is still on the shore.

3

Try the lightest with the heaviest

If people[left] + people[right] <= limit, the lightest joins the heaviest, so move left one step right. Either way the heaviest leaves in this boat, so move right one step left and add one to boats.

4

Stop when the pointers cross

Loop while left <= right. When the two pointers meet on the same index, one person is left, and they still need a boat of their own.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def numRescueBoats(self, people: List[int], limit: int) -> int:
▶3 people.sort()
▶4 left, right = 0, len(people) - 1
▶5 boats = 0
▶6 while left <= right:
▶7 if people[left] + people[right] <= limit:
▶8 left += 1
▶9 right -= 1
▶10 boats += 1
▶11 return boats
people as given3221boat3boats4 people, each boat holds 2 and at most 3
people4
limit3
The rules. A boat carries at most two people whose weights add up to at most 3. In this order there is no quick way to tell who should share.
people, lightest to heaviest1L223Rboat3boatssorted: lightest at L, heaviest at R
left0weight 1
right3weight 3
boats0
Sorting puts the lightest person still waiting at L and the heaviest at R. Each boat will be decided by those two alone.
people, lightest to heaviest1L223Rboat331boats31 + 3 is over 3 → 3 goes alone
L1index 0
R3index 3
boats1
Even the lightest person (1) is too heavy to join 3, so nobody can. The heaviest takes boat 1 alone, and L stays put for a lighter partner later.
people, lightest to heaviest1L22R3boat321boats31 + 21 + 2 fits 3 → share boat 2
L1index 0
R2index 2
boats2
The heaviest (2) and the lightest (1) weigh 3 together, within 3. Pairing them is safe: no other partner would leave more room for the people still waiting. Both pointers move in.
people, lightest to heaviest12L R23boat32boats31 + 22one person left → boat 3 alone
L2index 1
R2index 1
boats3
L and R meet: one person, weighing 2, is still waiting and nobody is left to share with. They take boat 3 on their own, which is why the loop runs while L ≤ R.
people, lightest to heaviest1223boat3boats31 + 22answer: 3 boats
boats3
shared1
Answer 3. 1 boat is shared. The other boats carry one person because nobody still waiting could share with them.
05

Common pitfalls

Pairing neighbours in sorted order

✗ Wrong
i = 0
while i < len(people):
    if i + 1 < len(people) and people[i] + people[i + 1] <= limit:
        i += 2
    else:
        i += 1
    boats += 1
✓ Right
if people[left] + people[right] <= limit:
    left += 1
right -= 1
boats += 1

Two light people sharing a boat can leave a heavy one with nobody light enough to join. On [1, 2, 2, 3] with limit = 4 this uses 3 boats (1+2, 2, 3); pairing 1 with 3 and 2 with 2 needs only 2.

Moving both pointers every time

✗ Wrong
left += 1
right -= 1
boats += 1
✓ Right
if people[left] + people[right] <= limit:
    left += 1
right -= 1
boats += 1

When the pair is too heavy, only the heaviest leaves; the light person is still waiting. On [3, 5, 3, 4] with limit = 5 nobody can share, so the answer is 4, but this returns 2.

Filling a boat with as many people as fit

✗ Wrong
while load + people[left] <= limit:
    load += people[left]
    left += 1
✓ Right
if people[left] + people[right] <= limit:
    left += 1

The weight limit is not the only rule; a boat also seats at most two. On [1, 1, 1] with limit = 3 all three fit by weight, but the answer is 2 boats.

06

Edge cases

Someone weighs exactly limit

Every weight is at least 1, so any pair including them is over the limit. They always take a boat alone, which the code does without a special case.

07

Complexity

Time
O(n log n)
Space
O(1)
Sorting costs O(n log n); the two-pointer pass after it is O(n). Apart from the sort's own working space, only two indexes and a counter are kept.
08

Boats to Save People FAQ

Can it run faster than O(n log n)?

Yes. Weights are at most 3 × 10⁴, so a counting sort, one bucket per weight, sorts in O(n + limit), and the same two-pointer pass works on the result. It only pays off when there are many people compared with the weight range.

What if a boat could carry three people?

Then the lightest-with-heaviest swap argument no longer holds, and the problem turns into a form of bin packing, which has no known fast exact method in general.