Skip to content

Commit 2a81891

Browse files
committed
Added Problem 349
1 parent a86bd2d commit 2a81891

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

intersection.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Given two arrays, write a function to compute their intersection.
3+
4+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
5+
Output: [2]
6+
"""
7+
8+
# Solution 1:
9+
10+
class Solution:
11+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
12+
return list(set(nums1) & set(nums2))
13+
14+
# Time complexity : O(n+m) in the average case and O(n×m) in the worst case when load factor is high enough.
15+
16+
# Space complexity : O(n+m) in the worst case when all elements in the arrays are different.
17+
18+
# Solution 2:
19+
20+
class Solution:
21+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
22+
hash = []
23+
for i in nums1:
24+
if i not in hash and i in nums2:
25+
hash.append(i)
26+
return hash
27+
28+
# Time Complexity = O(n) where n is the length of nums1
29+
# Space Complexity = O(n)

0 commit comments

Comments
 (0)