Couples Holding Hands
Master this topic with zero to advance depth.
Couples Holding Hands
There are couples sitting in seats. Every couple is assigned IDs . Return the minimum number of swaps so that every couple is sitting side by side.
Mathematical Insight
This is a permutation decomposition problem. If we have couples and they form cycles (connected components of incorrectly paired couples), the number of swaps needed is .
Examples
Level II: Greedy Swaps
Intuition
Iterate through the seats in pairs. For each seat and , if they are not already a couple, find where the partner of the person at seat is sitting and swap them into seat . This local greedy strategy is optimal.
Level III: Union-Find (Cycle Decomposition)
Intuition
Each pair of seats should hold a couple. If seat pair holds parts of couple and couple , union and . The number of swaps is total couples minus number of components.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.