백준 1269 대칭 차집합

김민영·2023년 1월 1일
0

알고리즘

목록 보기
20/125

계획

  • 이진 탐색으로 공통 원소 찾고, 두 집합 원소 합 - 공통 원소 개수 * 2
import sys
inp = sys.stdin.readline
A, B = map(int, input().split())
A_lst = list(map(int, input().split()))
B_lst = list(map(int, input().split()))
A_lst.sort()

def binSearch(s, e, t):
    while s <= e:
        m = (s + e) // 2
        if A_lst[m] == t:
            return 1
        elif A_lst[m] > t:
            e = m - 1
        else:
            s = m + 1
    return 0

ans = 0
for i in B_lst:
    ans += binSearch(0, A - 1, i)
print(A+B-ans*2)
  • 파이썬의 set은 차집합 기능이 있음~
import sys
input = sys.stdin.readline
A, B = map(int, input().split())
A_set = set(map(int, input().split()))
B_set = set(map(int, input().split()))
print(len(A_set-B_set) + len(B_set-A_set))
  • 두 방식 채점 결과의 메모리, 시간 차이

    위가 set 차집합 사용, 밑이 이진 탐색 구현
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글