백준 1920번 수 찾기

다혜·2022년 5월 16일
0

Algorithm

목록 보기
6/7

처음에 이렇게 풀었다...

N = int(input())
A = list(map(int,input().split(" ")))
M = int(input())
M_list = list(map(int,input().split(" ")))

for i in M_list:
  if i in A:
    print(1)
  else:
    print(0)

시간 초과가 났다.

알고보니 이진 탐색으로 풀어야한다고 😂


N = int(input())
A = list(map(int,input().split()))
M = int(input())
M_list = list(map(int,input().split()))

def ans(target, arr):
  start = 0
  end = len(arr)-1
  while start<=end:
    mid = (start+end)//2
    if target==arr[mid]:
      return 1
    elif target<arr[mid]:
      end = mid-1
    else:
      start=mid+1
  return 0

A.sort()
for i in M_list:
  print(ans(i,A))

수정 완 👍🏻

profile
봉식이를 위한 개발을 하고 싶오

0개의 댓글