visual walkthrough
Boats to Save People
Pairs the heaviest with the lightest; a greedy argument in two-pointer form.
The idea
The heaviest person is the hardest to place, so deal with them first. If even the lightest person can't share their boat, nobody can and they go alone. Otherwise pair them with the lightest, which wastes the least capacity.
Sorting plus two pointers makes that greedy choice in a single pass.
Complexity
| approach | time | space |
|---|---|---|
| Sort + two pointers | O(n log n) | O(1) |