-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0765-couples-holding-hands.js
46 lines (41 loc) · 1.32 KB
/
0765-couples-holding-hands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 765. Couples Holding Hands
* https://leetcode.com/problems/couples-holding-hands/
* Difficulty: Hard
*
* There are n couples sitting in 2n seats arranged in a row and want to hold hands.
*
* The people and seats are represented by an integer array row where row[i] is the ID of the
* person sitting in the ith seat. The couples are numbered in order, the first couple being
* (0, 1), the second couple being (2, 3), and so on with the last couple being (2n - 2, 2n - 1).
*
* Return the minimum number of swaps so that every couple is sitting side by side. A swap
* consists of choosing any two people, then they stand up and switch seats.
*/
/**
* @param {number[]} row
* @return {number}
*/
function minSwapsCouples(row) {
const n = row.length / 2;
const partner = new Array(2 * n);
const position = new Array(2 * n);
for (let i = 0; i < 2 * n; i++) {
partner[i] = i % 2 === 0 ? i + 1 : i - 1;
position[row[i]] = i;
}
let swaps = 0;
for (let i = 0; i < 2 * n; i += 2) {
const current = row[i];
const expected = partner[current];
const nextPerson = row[i + 1];
if (nextPerson !== expected) {
row[i + 1] = expected;
row[position[expected]] = nextPerson;
position[nextPerson] = position[expected];
position[expected] = i + 1;
swaps++;
}
}
return swaps;
}