Skip to content

Commit 50bd103

Browse files
authored
Update people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.py
1 parent 6cee4ac commit 50bd103

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Python/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Time: O(n * c * l + n^2 * c), n is favoriteCompanies.length
2-
# , c is the max of favoriteCompanies[i].length
1+
# Time: O(n * m * l + n^2 * m), n is favoriteCompanies.length
2+
# , m is the max of favoriteCompanies[i].length
33
# , l is the max of favoriteCompanies[i][j].length
4-
# Space: O(n * c * l)
4+
# Space: O(n * m * l)
55

66
class Solution(object):
77
def peopleIndexes(self, favoriteCompanies):
@@ -22,10 +22,10 @@ def peopleIndexes(self, favoriteCompanies):
2222

2323

2424

25-
# Time: O(n^2 * c * l * log*(n)), n is favoriteCompanies.length
26-
# , c is the max of favoriteCompanies[i].length
27-
# , l is the max of favoriteCompanies[i][j].length
28-
# Space: O(n * c * l)
25+
# Time: O(n * m * l + n^2 * m * log*(n)), n is favoriteCompanies.length
26+
# , m is the max of favoriteCompanies[i].length
27+
# , l is the max of favoriteCompanies[i][j].length
28+
# Space: O(n * m * l)
2929
class UnionFind(object):
3030
def __init__(self, data):
3131
self.data = [set(d) for d in data]
@@ -54,9 +54,16 @@ def peopleIndexes(self, favoriteCompanies):
5454
:type favoriteCompanies: List[List[str]]
5555
:rtype: List[int]
5656
"""
57-
union_find = UnionFind(favoriteCompanies)
58-
for i in xrange(len(favoriteCompanies)):
59-
for j in xrange(len(favoriteCompanies)):
57+
lookup, comps = {}, []
58+
for cs in favoriteCompanies:
59+
comps.append(set())
60+
for c in cs:
61+
if c not in lookup:
62+
lookup[c] = len(lookup)
63+
comps[-1].add(lookup[c])
64+
union_find = UnionFind(comps)
65+
for i in xrange(len(comps)):
66+
for j in xrange(len(comps)):
6067
if j == i:
6168
continue
6269
union_find.union_set(i, j)

0 commit comments

Comments
 (0)