본문 바로가기

알고리즘 문제풀이

백준 1920 수 찾기 Python 이진탐색 실버 4

파이썬은 in 이 있어서 만약 썼다면 set으로 바꿔서 중복값을 없앤 후 찾았다면 실행시간이 조금 더 빨랐을 것같다.

 

# 수 찾기 실버 4 1920
def find_num(v):
    s = 0
    e = n
    if v < arr[s] or v > arr[e-1]:
        return 0
    while s <= e:
        half = (s + e) // 2
        if arr[half] == v:

            return 1
        elif arr[half] < v:
            s = half + 1
        else: # 탐색 중인 값이 찾는 값보다 클 때
            e = half -1
    return 0

n = int(input())
arr = list(map(int,input().split()))
arr.sort()
m = int(input())
find = list(map(int,input().split()))
for i in find:
    print(find_num(i))