Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Couples Holding Hands

Minimum swaps

2n2n people sit in a row. Couples are numbered (0,1),(2,3),..,(2n2,2n1)(0,1), (2,3), .., (2n-2, 2n-1). Find the minimum swaps to seat every couple together. For [0,2,1,3][0,2,1,3], one swap (2,1)(2,1) gives [0,1,2,3][0,1,2,3]. Answer is 11.

Here's a cycle-counting problem disguised as greedy.

Think about the relationship between people and positions.