파이썬은 in 이 있어서 만약 썼다면 set으로 바꿔서 중복값을 없앤 후 찾았다면 실행시간이 조금 더 빨랐을 것같다.
# 수 찾기 실버 4 1920
def find_num(v):
s = 0
e = n
if v < arr[s] or v > arr[e-1]:
return 0
while s <= e:
half = (s + e) // 2
if arr[half] == v:
return 1
elif arr[half] < v:
s = half + 1
else: # 탐색 중인 값이 찾는 값보다 클 때
e = half -1
return 0
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
m = int(input())
find = list(map(int,input().split()))
for i in find:
print(find_num(i))
'알고리즘 문제풀이' 카테고리의 다른 글
백준 5014 스타트링크 Python BFS 실버 1 (2) | 2024.03.31 |
---|---|
백준 1193 분수 찾기 Python 수학 실버 5 (0) | 2024.03.29 |
백준 1260 DFS 와 BFS Python 실버 2 (0) | 2024.03.28 |
백준 15657 N과 M (8) Python 백트래킹 실버 3 (1) | 2024.03.27 |
백준 1009 분산처리 Python 브론즈2 (0) | 2024.03.27 |