Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Rescue Pairs

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Pair the heaviest person with the lightest possible partner: The Rescue Pairs.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Two-ended greedy pairing matches the clues
  2. 2Keep this true after every move: every unassigned person lies between light and heavy, and each counted boat is unavoidable
  3. 3Reach the result within O(n log n)

Rules and pressure

  • Target cost: O(n log n)
  • State rule: every unassigned person lies between light and heavy, and each counted boat is unavoidable
Lesson 1 of 3

Live algorithm trace

Two-ended greedy pairing

Complete execution
1 of 5

Sort weights and place pointers at the lightest and heaviest people.

1sort weights
2light = 0; heavy = n - 1
3count one boat for heavy
4if light + heavy <= limit: light += 1
5heavy -= 1
6return boats
limit = 4boats = 0
Truth to preserve / Cost target
Truth to preserve

every unassigned person lies between light and heavy, and each counted boat is unavoidable

Cost target

O(n log n)

The heaviest remaining person must take a boat. If they can pair with the lightest, that pairing cannot hurt any future solution; otherwise they cannot pair with anyone and must go alone.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead