Couples Holding Hands Problem
Description
LeetCode Problem 765.
There are n couples sitting in 2n seats arranged in a row and want to hold hands.
The people and seats are represented by an integer array row where row[i] is the ID of the person sitting in the i^th seat. The couples are numbered in order, the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2n - 2, 2n - 1).
Return the minimum number of swaps so that every couple is sitting side by side. A swap consists of choosing any two people, then they stand up and switch seats.
Example 1:
Input: row = [0,2,1,3]
Output: 1
Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
Example 2:
Input: row = [3,2,0,1]
Output: 0
Explanation: All couples are already seated side by side.
Constraints:
- 2n == row.length
- 2 <= n <= 30
- n is even.
- 0 <= row[i] < 2n
- All the elements of row are unique.
Solution
Approach: Greedy Swaps with a Seat Index
Look at the seats in pairs: (0, 1), (2, 3), and so on. For each pair, keep the person in the left seat and make sure their partner sits on the right. If the partner is not there, swap the partner in with one swap. Every pair is then fixed exactly once, from left to right.
- The partner of person
xisx ^ 1: flipping the lowest bit turns2kinto2k + 1and back. - Keep
seatOf[person]so the partner can be found in O(1). After a swap, update both the array andseatOffor the two people who moved.
Why this is optimal. Draw a graph whose nodes are the couples, with an edge between two couples for every seat pair they share. Every couple has degree 2, so the graph splits into c cycles. A swap changes only two seat pairs, so it can split a cycle into at most two pieces: each swap adds at most one cycle. At the end every couple is a cycle on its own, so at least n - c swaps are needed, where n is the number of couples. The greedy uses exactly that many: on a cycle of k couples, each swap seats one couple and leaves a cycle of k - 1, and the last swap of the cycle seats its final two couples together.
The detail people get wrong is forgetting to update seatOf for the stranger who moved into the partner's old seat; a later step then looks for them in the wrong place.
Complexity: O(n) time and O(n) space, where n is the number of couples (row has 2n seats).
class Solution:
def minSwapsCouples(self, row: List[int]) -> int:
seat_of = [0] * len(row) # person -> the seat they are in now
for seat, person in enumerate(row):
seat_of[person] = seat
swaps = 0
for seat in range(0, len(row), 2):
partner = row[seat] ^ 1 # couples are (0, 1), (2, 3), ...: flipping the last bit gives the partner
if row[seat + 1] != partner:
# Bring the partner next to row[seat], and move the stranger to the partner's old seat.
partner_seat = seat_of[partner]
stranger = row[seat + 1]
row[seat + 1], row[partner_seat] = partner, stranger
seat_of[partner], seat_of[stranger] = seat + 1, partner_seat
swaps += 1
return swaps
Now write it from memory
Close the write-up and solve it yourself against the tests.