forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-out-of-three.py
43 lines (36 loc) · 1.03 KB
/
two-out-of-three.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
# Time: O(n)
# Space: O(min(n, r)), r is the range size of nums
import collections
class Solution(object):
def twoOutOfThree(self, nums1, nums2, nums3):
"""
:type nums1: List[int]
:type nums2: List[int]
:type nums3: List[int]
:rtype: List[int]
"""
K = 2
cnt = collections.Counter()
for nums in nums1, nums2, nums3:
cnt.update(set(nums))
return [x for x, c in cnt.iteritems() if c >= K]
# Time: O(n)
# Space: O(min(n, r)), r is the range size of nums
import collections
class Solution2(object):
def twoOutOfThree(self, nums1, nums2, nums3):
"""
:type nums1: List[int]
:type nums2: List[int]
:type nums3: List[int]
:rtype: List[int]
"""
K = 2
cnt = collections.Counter()
result = []
for nums in nums1, nums2, nums3:
for x in set(nums):
cnt[x] += 1
if cnt[x] == K:
result.append(x)
return result