10815번 : 숫자 카드

김민관·2022년 9월 23일

백준_Silver

목록 보기
46/57

문제

파이썬

def binary(array, target):
    left = 0
    right = len(array) - 1

    while left <= right:
        mid = (left+right)//2
        if array[mid] == target:
            return 1
        elif array[mid] > target:
            right = mid - 1
        else:
            left = mid + 1

    return 0


n = int(input())

cards = list(map(int, input().split()))
cards.sort()

m = int(input())

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

answer = []

for i in nums:
    answer.append(binary(cards, i))

print(*answer)

풀이

  • 이진 탐색을 통해 확인
  • python 내부 라이브러리에 bisect라는 이진탐색 라이브러리가 있음
profile
게임 개발일지 & IT 소식들 공유

0개의 댓글