Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit be16094

Browse files
author
Khanh Do
committed
Finish 1452
1 parent 1ec2fd2 commit be16094

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
3+
def people_indexes(favorite_companies):
4+
"""Return the indices of people whose list of favorite companies is not a
5+
subset of any other list of favorite companies.
6+
7+
Time: O(n^2) -- nested loops to compare current list with all other lists
8+
Space: O(n) -- all lists may be disjoint with one another
9+
"""
10+
elim_indexes = set()
11+
12+
for i, u in enumerate(favorite_companies):
13+
for j, v in enumerate(favorite_companies):
14+
if j != i: # Exclude self-comparison
15+
if set(u).issubset(set(v)):
16+
elim_indexes.add(i)
17+
18+
all_indexes = set(range(len(favorite_companies)))
19+
return list(all_indexes - elim_indexes)
20+
21+
22+
def main():
23+
example_cases = [
24+
(
25+
[
26+
['leetcode', 'google', 'facebook'],
27+
['google', 'microsoft'],
28+
['google', 'facebook'],
29+
['google'],
30+
['amazon'],
31+
],
32+
[0, 1, 4]),
33+
(
34+
[
35+
['leetcode', 'google', 'facebook'],
36+
['leetcode', 'amazon'],
37+
['facebook', 'google'],
38+
],
39+
[0, 1]
40+
),
41+
(
42+
[
43+
['leetcode'],
44+
['google'],
45+
['facebook'],
46+
['amazon'],
47+
],
48+
[0, 1, 2, 3]
49+
),
50+
51+
]
52+
53+
for favorite_companies, ans in example_cases:
54+
assert people_indexes(favorite_companies) == ans
55+
56+
57+
corner_cases = [
58+
(
59+
[], # len(favorite_companies) == 0
60+
[]
61+
),
62+
(
63+
[['google']], # len(favorite_companies) == 1
64+
[0]
65+
),
66+
]
67+
68+
for favorite_companies, ans in corner_cases:
69+
assert people_indexes(favorite_companies) == ans
70+
71+
72+
if __name__ == '__main__':
73+
main()

0 commit comments

Comments
 (0)