백준(10815) - 숫자 카드 Python

최대 1 분 소요

백준(10815) - 숫자 카드

문제풀이: 해쉬, 파이썬

해결방법

예시)

# 입력 #
5
6 3 2 10 -10
8
10 9 -5 2 3 4 5 -10

# 출력결과 #
1 0 0 1 1 0 0 1
  • 백준 사이트에서는 이분탐색 문제로 분류되어 있지만 해쉬 알고리즘으로 푸는 방식이 시간 효율이 좋다.
  • 파이썬 dict를 사용하면 쉽게 해결이 가능하다.
input()
N = map(int, input().split())
input()
M = map(int, input().split())
hashmap = {}
for n in N:
    if n in hashmap:
        hashmap[n] += 1
    else:
        hashmap[n] = 1

for m in M:
    if m in hashmap:
        print(1, end=" ")
    else:
        print(0, end=" ")

댓글남기기