-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcouples-holding-hands.py
More file actions
35 lines (24 loc) · 919 Bytes
/
couples-holding-hands.py
File metadata and controls
35 lines (24 loc) · 919 Bytes
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
import math
from typing import List, Set
class Solution:
def minSwapsCouples(self, row: List[int]) -> int:
reverse_index: List[Set[int]] = [set() for _ in range(len(row) // 2)]
for seat in range(len(row)):
reverse_index[row[seat] // 2].add(seat)
visited = [False] * (len(row) // 2)
min_swaps = 0
for init_seat in range(len(row)):
if visited[init_seat // 2]:
continue
visited[init_seat // 2] = True
loop_len = 0
seat = init_seat
while (
seat := (reverse_index[row[seat] // 2] - {seat}).pop()
) // 2 != init_seat // 2:
visited[seat // 2] = True
seat = (seat // 2 + 1) * 2 - 1 - seat % 2
visited[seat // 2] = True
loop_len += 1
min_swaps += loop_len
return min_swaps