백준 1920번 "수 찾기"

sanha_OvO·2021년 6월 10일
0

Algorithm

목록 보기
44/84

문제

백준 1920번 수찾기


풀이


기본적인 이분 탐색으로 풀 수 있는 문제

파이썬의 경우 set자료형을 이용하여 풀면 더 쉽고 효율적으로 풀 수 있지만
연습을 위해 이분 탐색으로 문제를 풀었다.


Python 코드

import sys
input = sys.stdin.readline

def bin_search(l, k):
  left = 0
  right = len(l) - 1

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

n = int(input())
nlist = sorted(list(map(int, input().split())))
m = int(input())
mlist = list(map(int, input().split()))
for i in range(m):
  print(bin_search(nlist, mlist[i]))

profile
Web Developer / Composer

0개의 댓글