Hanbit the Developer

[탐색] Python - 1920번 수 찾기 본문

Algorithm/백준

[탐색] Python - 1920번 수 찾기

hanbikan 2021. 2. 24. 09:18
def isThere(List, num):
    left, right = 0, len(List)-1
    while left <= right:
        mid = (left+right)//2
        if List[mid]>num:
            right = mid - 1
        elif List[mid]<num:
            left = mid + 1
        else:
            return 1
    return 0
    
N = int(input())
A = list(map(int, input().split()))
M = int(input())
B = list(map(int, input().split()))
A.sort()

for b in B:
    print(isThere(A, b))

사실, 더 빠른 풀이들이 있으나(가령, Dictionary를 통해 Index로 바로 접근하기 등)

해당 문제의 난이도가 낮으므로 정석적인 풀이를 지향하였다.