[백준] 10815 숫자 카드

파이톨치·2022년 8월 8일
0

백준

목록 보기
3/12

코드

# N개의 정수를 입력 받기
n = int(input())

a = list(map(int, input().split()))

# M개의 정수를 입력 받기
m = int(input())

b = list(map(int, input().split()))

# 정렬하기
a.sort()

# 탐색하기
def binary_search(a, alpha):
    start = 0
    end   = len(a) - 1

    while end >= start:
        mid   = (start + end) // 2 
        target = a[mid]
        
        if alpha == target:
            return 1
        elif alpha < target:
            end = mid - 1
        elif alpha > target:
            start = mid + 1
    
    return 0

# 출력하기
for alpha in b:
    print(binary_search(a, alpha), end=" ")

해결 방법 : 이진 탐색을 통해 시간 복잡도 줄인다.

문제 링크

https://www.acmicpc.net/problem/10815

유사 문제

https://www.acmicpc.net/problem/1920

profile
안알랴줌

0개의 댓글