- λμ²λΌ Hash Table λ°©μμΌλ‘ νλ κ² Two Pointer λ°©μμΌλ‘ νλ κ²λ³΄λ€ λ μ¬μ΄ κ² κ°λ€.
Solution - Brute Force, $O(N^2)$
from typing import List
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
answer = set()
for n1 in nums1:
for n2 in nums2:
if n1 == n2:
answer.add(n1)
return list(answer)
Solution - Hash Table, $O(N)$
from typing import List
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
answer = set()
num1_dic = {n1 for n1 in nums1}
for n2 in nums2:
if n2 in num1_dic:
answer.add(n2)
return answer
Reference
Leave a comment