Couples Holding Hands

Given a row of people sitting in seats, return the minimum number of swaps needed so that each couple sits together. DSU can group seats by connected couple relationships and count how many swaps each component requires.

Input Format

row = people seated in order

Output Format

minimum number of swaps required

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

row = [0,2,1,3]

Output:

1

Explanation:

Swapping 2 and 1 places both couples together.

Example 2:

Input:

row = [3,2,0,1]

Output:

0

Explanation:

Each couple is already seated together.

Example 3:

Input:

row = [0,1,2,3]

Output:

0

Explanation:

All couples are already adjacent.

Loading...
Couples Holding Hands - Union Find DSA Problem