#19 백준 1920번 파이썬

less than 1 minute read

우보만리

  • binary search 알고리즘을 활용하는 문제
  • 블로그를 보고 공부를 했다.

문제 link

## 이진 탐색 알고리즘
def binary_search(arr, target, low, high):
mid = (high + low)//2
if high >= low:
if arr[mid] == target:
return 1
elif arr[mid] > target:
return binary_search(arr, target, low, mid - 1)
else:
return binary_search(arr, target, mid+1, high)
else:
return 0
N = int(input())
A = sorted(list(map(int, input().split())))
M = int(input())
candidate = list(map(int,input().split()))
for m in candidate:
print(binary_search(A,m,0, len(A)-1))
## Run time : 720 ms
## Memory : 44992 KB
view raw 1920.py hosted with ❤ by GitHub