1113. Couples Holding Hands — Minimum Swaps

HardGraphsDSUGreedyGraph

There are n couples sitting in 2n seats in a row, given as 'row' where row[i] is the person in seat i. Couples are (0,1), (2,3), ..., (2k, 2k+1). In one swap you exchange any two people. Return the minimum number of swaps so that every couple sits in adjacent seats. The input is JSON {row}.

Input: JSON {row}.

Output: Integer — the minimum number of swaps.

Examples

Example 1
Input: {"row":[0,2,1,3]}
Output: 1
Explanation: One swap seats both couples together.
Example 2
Input: {"row":[3,2,0,1]}
Output: 0
Explanation: Already paired.
Example 3
Input: {"row":[0,1]}
Output: 0
Explanation: A single couple already adjacent.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →