문제 링크
1920번: 수 찾기
첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들
www.acmicpc.net
풀이
import sys
n = int(sys.stdin.readline())
arr = sorted(list(map(int, sys.stdin.readline().split())))
m = int(sys.stdin.readline())
arr2 = list(map(int, sys.stdin.readline().split()))
def search(num, arr):
high = len(arr)-1
low = 0
while low <= high:
mid = (high + low) // 2
item = arr[mid]
if item > num:
high = mid - 1
elif item < num:
low = mid + 1
else:
return 1
return 0
for i in arr2:
print(search(i, arr))
이진 탐색을 이용하여 해결하였다.
'연습장 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 1002] 터렛 with Python (0) | 2021.03.31 |
---|---|
[백준 5585] 거스름돈 with Node.js (0) | 2021.03.31 |
[백준 2231] 분해합 with Node.js (0) | 2021.03.30 |
[백준 9012] 괄호 with Python (0) | 2021.03.30 |
[백준 10828] 스택 with Python (0) | 2021.03.26 |