#765
Couples Holding Hands
expert · 1200 · lc hard +32 · verified · 59.2% accepted · 2,478 likes · top 56%
Description
There are n couples occupying 2n seats in a row, wanting to sit adjacent to their partner. People are identified by IDs and couples are paired as (0,1), (2,3), ..., (2n-2, 2n-1). The initial seating is given by the array row.
In one swap you may choose any two people and exchange their seats. Return the minimum number of swaps needed so that every couple sits side by side.
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.
Code
1
2
3