The Question
CodingCouples Holding Hands
There are $n$ couples sitting in $2n$ seats arranged in a row. 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 defined by their IDs: the first couple is $(0, 1)$, the second couple is $(2, 3)$, and so on, with the last couple being $(2n - 2, 2n - 1)$.
You want to rearrange the people so that every couple is sitting side by side (i.e., in seats $(0, 1), (2, 3), \dots$). A single swap consists of choosing any two people currently in seats and having them switch places. Return the minimum number of swaps required to achieve this state.
Constraints:
- 2n == row.length
- 2 <= n <= 30
- row is a permutation of [0, 2n - 1].Java
Greedy
Union-Find