-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_out_of_three.py
More file actions
80 lines (60 loc) · 1.92 KB
/
two_out_of_three.py
File metadata and controls
80 lines (60 loc) · 1.92 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
2032. Two Out of Three
Difficulty:Easy
Given three integer arrays nums1, nums2, and nums3, return a distinct array containing all the values that are present in at least two out of the three arrays.
You may return the values in any order.
Example 1:
Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
Output: [3,2]
Explanation: The values that are present in at least two arrays are:
- 3, in all three arrays.
- 2, in nums1 and nums2.
Example 2:
Input: nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
Output: [2,3,1]
Explanation: The values that are present in at least two arrays are:
- 2, in nums2 and nums3.
- 3, in nums1 and nums2.
- 1, in nums1 and nums3.
Example 3:
Input: nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
Output: []
Explanation: No value is present in at least two arrays.
Constraints:
1 <= nums1.length, nums2.length, nums3.length <= 100
1 <= nums1[i], nums2[j], nums3[k] <= 100
"""
from typing import List
class Solution:
def twoOutOfThree(
self, nums1: List[int], nums2: List[int], nums3: List[int]
) -> List[int]:
res = []
if len(nums1) > len(nums2):
for i in nums2:
if i in nums1:
res.append(i)
else:
for i in nums1:
if i in nums2:
res.append(i)
if len(nums1) > len(nums3):
for i in nums3:
if i in nums1:
res.append(i)
else:
for i in nums1:
if i in nums3:
res.append(i)
if len(nums2) > len(nums3):
for i in nums3:
if i in nums2:
res.append(i)
else:
for i in nums2:
if i in nums3:
res.append(i)
return list(set(res))
if __name__ == "__main__":
solution = Solution()
print(solution.twoOutOfThree(nums1=[1, 1, 3, 2], nums2=[2, 3], nums3=[3]))