💡문제접근

  • 이분 탐색을 이용해서 문제를 해결할 수 있었다.

💡코드(메모리 : 187272KB, 시간 : 5352ms)

def binary_search(target, data):
    start = 0
    end = len(data) - 1
    while True:
        mid = (start + end) // 2
        if start > end:
            break
        if data[mid] == target:
            return 1
        elif data[mid] > target:
            end = mid - 1
        else:
            start = mid + 1
    return 0

T = int(input())
for _ in range(T):
    N = int(input())
    note1 = list(map(int, input().split()))
    M = int(input())
    note2 = list(map(int, input().split()))

    note1.sort()
    for i in note2:
        if binary_search(i, note1) == 1:
            print(1)
        else:
            print(0)

📌 이분 탐색을 실행하기 위한 조건 : 반드시 정렬이 수행된 상태여야 한다.

💡소요시간 : 8m

0개의 댓글