Skip to content

Commit c16cf49

Browse files
authored
Update intersection-of-multiple-arrays.py
1 parent 3af188b commit c16cf49

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Python/intersection-of-multiple-arrays.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
# Time: O(n * l + r), n = len(nums), l = len(nums[0])
2+
# Space: O(r), r = max(nums)-min(nums)
3+
4+
# counting sort
5+
class Solution(object):
6+
def intersection(self, nums):
7+
"""
8+
:type nums: List[List[int]]
9+
:rtype: List[int]
10+
"""
11+
MAX_NUM = 1000
12+
cnt = [0]*(MAX_NUM+1)
13+
for num in nums:
14+
for x in num:
15+
cnt[x] += 1
16+
return [i for i in xrange(1, MAX_NUM+1) if cnt[i] == len(nums)]
17+
18+
119
# Time: O(n * l + r), n = len(nums), l = len(nums[0]), r = max(nums)-min(nums)
220
# Space: O(l)
3-
421
# set, counting sort
5-
class Solution(object):
22+
class Solution2(object):
623
def intersection(self, nums):
724
"""
825
:type nums: List[List[int]]
@@ -17,7 +34,7 @@ def intersection(self, nums):
1734
# Time: O(n * l + llogl), n = len(nums), l = len(nums[0])
1835
# Space: O(l)
1936
# set, sort
20-
class Solution2(object):
37+
class Solution3(object):
2138
def intersection(self, nums):
2239
"""
2340
:type nums: List[List[int]]

0 commit comments

Comments
 (0)