forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.py
70 lines (62 loc) · 2.49 KB
/
people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.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
# Time: O(n * m * l + n^2 * m), n is favoriteCompanies.length
# , m is the max of favoriteCompanies[i].length
# , l is the max of favoriteCompanies[i][j].length
# Space: O(n * m * l)
class Solution(object):
def peopleIndexes(self, favoriteCompanies):
"""
:type favoriteCompanies: List[List[str]]
:rtype: List[int]
"""
lookup, comps = {}, []
for cs in favoriteCompanies:
comps.append(set())
for c in cs:
if c not in lookup:
lookup[c] = len(lookup)
comps[-1].add(lookup[c])
return [i for i, c1 in enumerate(comps)
if not any(i != j and len(c1) < len(c2) and c1 < c2
for j, c2 in enumerate(comps))]
# Time: O(n * m * l + n^2 * m * log*(n)), n is favoriteCompanies.length
# , m is the max of favoriteCompanies[i].length
# , l is the max of favoriteCompanies[i][j].length
# Space: O(n * m * l)
class UnionFind(object):
def __init__(self, data):
self.data = [set(d) for d in data]
self.set = range(len(data))
def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x]
def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root == y_root:
return
if len(self.data[x_root]) > len(self.data[y_root]) and \
self.data[x_root] > self.data[y_root]:
self.set[y_root] = x_root
elif len(self.data[x_root]) < len(self.data[y_root]) and \
self.data[x_root] < self.data[y_root]:
self.set[x_root] = y_root
class Solution2(object):
def peopleIndexes(self, favoriteCompanies):
"""
:type favoriteCompanies: List[List[str]]
:rtype: List[int]
"""
lookup, comps = {}, []
for cs in favoriteCompanies:
comps.append(set())
for c in cs:
if c not in lookup:
lookup[c] = len(lookup)
comps[-1].add(lookup[c])
union_find = UnionFind(comps)
for i in xrange(len(comps)):
for j in xrange(len(comps)):
if j == i:
continue
union_find.union_set(i, j)
return [x for i, x in enumerate(union_find.set) if x == i]