-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathminimize-hamming-distance-after-swap-operations.py
94 lines (84 loc) · 2.8 KB
/
minimize-hamming-distance-after-swap-operations.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Time: O(n)
# Space: O(n)
class Solution(object):
def minimumHammingDistance(self, source, target, allowedSwaps):
"""
:type source: List[int]
:type target: List[int]
:type allowedSwaps: List[List[int]]
:rtype: int
"""
def iter_flood_fill(adj, node, lookup, idxs):
stk = [node]
while stk:
node = stk.pop()
if node in lookup:
continue
lookup.add(node)
idxs.append(node)
for child in adj[node]:
stk.append(child)
adj = [set() for i in xrange(len(source))]
for i, j in allowedSwaps:
adj[i].add(j)
adj[j].add(i)
result = 0
lookup = set()
for i in xrange(len(source)):
if i in lookup:
continue
idxs = []
iter_flood_fill(adj, i, lookup, idxs)
source_cnt = collections.Counter([source[i] for i in idxs])
target_cnt = collections.Counter([target[i] for i in idxs])
diff = source_cnt-target_cnt
result += sum(diff.itervalues())
return result
# Time: O(n * α(n)) ~= O(n)
# Space: O(n)
import collections
class UnionFind(object): # Time: O(n * α(n)), Space: O(n)
def __init__(self, n):
self.set = range(n)
self.rank = [0]*n
def find_set(self, x):
stk = []
while self.set[x] != x: # path compression
stk.append(x)
x = self.set[x]
while stk:
self.set[stk.pop()] = x
return x
def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root == y_root:
return False
if self.rank[x_root] < self.rank[y_root]: # union by rank
self.set[x_root] = y_root
elif self.rank[x_root] > self.rank[y_root]:
self.set[y_root] = x_root
else:
self.set[y_root] = x_root
self.rank[x_root] += 1
return True
class Solution2(object):
def minimumHammingDistance(self, source, target, allowedSwaps):
"""
:type source: List[int]
:type target: List[int]
:type allowedSwaps: List[List[int]]
:rtype: int
"""
uf = UnionFind(len(source))
for x, y in allowedSwaps:
uf.union_set(x, y)
groups = collections.defaultdict(set)
for i in xrange(len(source)):
groups[uf.find_set(i)].add(i)
result = 0
for idxs in groups.itervalues():
source_cnt = collections.Counter([source[i] for i in idxs])
target_cnt = collections.Counter([target[i] for i in idxs])
diff = source_cnt-target_cnt
result += sum(diff.itervalues())
return result