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.
- 1 <= people.length <= 5 * 10⁴
- 1 <= people[i] <= limit <= 3 * 10⁴
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.
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.
Approach
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.
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.
Point at both ends
Set left = 0, right = len(people) - 1 and boats = 0. Everyone from left to right is still on the shore.
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.
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.
Code in Python, C++ and Java
Common pitfalls
Pairing neighbours in sorted order
i = 0
while i < len(people):
if i + 1 < len(people) and people[i] + people[i + 1] <= limit:
i += 2
else:
i += 1
boats += 1if people[left] + people[right] <= limit:
left += 1
right -= 1
boats += 1Two 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
left += 1 right -= 1 boats += 1
if people[left] + people[right] <= limit:
left += 1
right -= 1
boats += 1When 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
while load + people[left] <= limit:
load += people[left]
left += 1if people[left] + people[right] <= limit:
left += 1The 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.
Edge cases
limitEvery 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.
Complexity
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.