Data Structures & AlgorithmsVery hard

Why does pairing the heaviest remaining person with the lightest one (when they fit together) minimise boats when each boat carries at most two people?

  1. A. If the heaviest cannot pair with the lightest, it cannot pair with anyone, so it must ride alone; otherwise pairing it with the lightest frees the most capacity for others.
  2. B. Heavier people always need their own boat.
  3. C. Pairing the two heaviest is always the best choice.
  4. D. The lightest person should always ride alone.

Correct answer: A. If the heaviest cannot pair with the lightest, it cannot pair with anyone, so it must ride alone; otherwise pairing it with the lightest frees the most capacity for others.

The heaviest person is the hardest to place, so if any partner fits it is the lightest one, and using that partner leaves the most room for the rest. This exchange argument makes the two-pointer greedy optimal.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic