Solved LeetCode program · 542 of 1,000
#0765 · Couples Holding Hands
Original English source
Keep a shared seats holding 0, 2, 1, and 3. Keep a shared last of 0. The end is the last position of seats. Set last to end. Keep a shared swaps of 0.
To pairseat with seat (a whole number). The first is item seat of seats. The mate is first plus 1. If first modulo 2 is 1, set mate to first minus 1. The second is item (seat plus 1) of seats. If second is mate, return. The found is seat plus 1. For every probe from seat plus 2 to last, if item probe of seats is mate, set found to probe. swap item (seat plus 1) of seats and item found of seats. Add 1 to swaps.
For every seat from 0 to last by 2.
Pairseat with seat.
Show swaps.
Expected stdout
1