Skip to content

visual walkthrough

Boats to Save People

MediumSort + Greedy PairingReported at: AmazonAppleMicrosoft+4

Pairs the heaviest with the lightest; a greedy argument in two-pointer form.

Solve on LeetCode

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

approachtimespace
Sort + two pointersO(n log n)O(1)

More walkthroughs