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
3
3
# , l is the max of favoriteCompanies[i][j].length
4
- # Space: O(n * c * l)
4
+ # Space: O(n * m * l)
5
5
6
6
class Solution (object ):
7
7
def peopleIndexes (self , favoriteCompanies ):
@@ -22,10 +22,10 @@ def peopleIndexes(self, favoriteCompanies):
22
22
23
23
24
24
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)
29
29
class UnionFind (object ):
30
30
def __init__ (self , data ):
31
31
self .data = [set (d ) for d in data ]
@@ -54,9 +54,16 @@ def peopleIndexes(self, favoriteCompanies):
54
54
:type favoriteCompanies: List[List[str]]
55
55
:rtype: List[int]
56
56
"""
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 )):
60
67
if j == i :
61
68
continue
62
69
union_find .union_set (i , j )
0 commit comments