Community Problem

Boats to Save People

Difficulty: Medium

Given person weights, a boat capacity, and a 2-person-per-boat limit, find the minimum number of boats.

Boats to Save People

Given person weights, a boat capacity, and a 2-person-per-boat limit, find the minimum number of boats.

MEDIUM
Free
greedy
two-pointers
sorting
chloesaeed

By @chloesaeed

May 13, 2026

·

Updated May 18, 2026

179 views

4

Rate

I had this on a phone screen and the interviewer's hint was "why does pairing the heaviest with the lightest never lose?". That hint is the entire interview signal: the greedy is non-obvious because at first it looks like an ILP problem, but the exchange argument boils down to one line. The catalog covers Two Pointers but skipped this one, which is the cleanest greedy + two-pointers fusion problem in interview rotation.

Boats to Save People

You are given an array people where people[i] is the weight of the i-th person, and an integer limit (the max weight a single boat can carry). Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person.

Examples

Example 1:

  • Input: people = [1, 2], limit = 3
  • Output: 1
  • Explanation: One boat carries both.

Example 2:

  • Input: people = [3, 2, 2, 1], limit = 3
  • Output: 3
  • Explanation: Boat 1 carries 1 + 2, boat 2 carries 2, boat 3 carries 3.

Example 3:

  • Input: people = [3, 5, 3, 4], limit = 5
  • Output: 4
  • Explanation: No two of these weights sum to <= 5. Every person needs their own boat.

Example 4:

  • Input: people = [5, 1, 4, 2], limit = 6
  • Output: 2
  • Explanation: Boat 1 carries 5 + 1, boat 2 carries 4 + 2.

Constraints

  • 1 <= people.length <= 5 * 10^4.
  • 1 <= people[i] <= limit <= 3 * 10^4.

Follow-up

Why is the greedy "pair heaviest with lightest if they fit, otherwise the heaviest goes alone" optimal? The exchange argument is short: in any optimal solution, if the heaviest person sails alone but COULD have been paired with the lightest, we can replace that solo trip with a pair without increasing the boat count.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems