Skip to content

Commit 7a2aff3

Browse files
add 765
1 parent 615eda4 commit 7a2aff3

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 765. Couples Holding Hands
5+
*
6+
* N couples sit in 2N seats arranged in a row and want to hold hands.
7+
* We want to know the minimum number of swaps so that every couple is sitting side by side.
8+
* A swap consists of choosing any two people, then they stand up and switch seats.
9+
* The people and seats are represented by an integer from 0 to 2N-1, the couples are numbered in order,
10+
* the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2N-2, 2N-1).
11+
* The couples' initial seating is given by row[i] being the value of the person who is initially sitting in the i-th seat.
12+
13+
Example 1:
14+
15+
Input: row = [0, 2, 1, 3]
16+
Output: 1
17+
Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
18+
19+
Example 2:
20+
21+
Input: row = [3, 2, 0, 1]
22+
Output: 0
23+
Explanation: All couples are already seated side by side.
24+
25+
Note:
26+
len(row) is even and in the range of [4, 60].
27+
row is guaranteed to be a permutation of 0...len(row)-1.
28+
*/
29+
30+
public class _765 {
31+
public static class Solution1 {
32+
public int minSwapsCouples(int[] row) {
33+
int swaps = 0;
34+
for (int i = 0; i < row.length - 1; i += 2) {
35+
int coupleValue = row[i] % 2 == 0 ? row[i] + 1 : row[i] - 1;
36+
if (row[i + 1] != coupleValue) {
37+
swaps++;
38+
int coupleIndex = findIndex(row, coupleValue);
39+
swap(row, coupleIndex, i + 1);
40+
}
41+
}
42+
return swaps;
43+
}
44+
45+
private void swap(int[] row, int i, int j) {
46+
int tmp = row[i];
47+
row[i] = row[j];
48+
row[j] = tmp;
49+
}
50+
51+
private int findIndex(int[] row, int value) {
52+
for (int i = 0; i < row.length; i++) {
53+
if (row[i] == value) {
54+
return i;
55+
}
56+
}
57+
return -1;
58+
}
59+
}
60+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._765;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _765Test {
10+
private static _765.Solution1 solution1;
11+
private static int[] row;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _765.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
row = new int[] {0, 2, 1, 3};
21+
assertEquals(1, solution1.minSwapsCouples(row));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
row = new int[] {3, 2, 0, 1};
27+
assertEquals(0, solution1.minSwapsCouples(row));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
row = new int[] {0, 4, 7, 3, 1, 5, 2, 8, 6, 9};
33+
assertEquals(3, solution1.minSwapsCouples(row));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
row = new int[] {5, 6, 4, 0, 2, 1, 9, 3, 8, 7, 11, 10};
39+
assertEquals(4, solution1.minSwapsCouples(row));
40+
}
41+
}

0 commit comments

Comments
 (0)