1번 코드
집합을 이용한 풀이. set 자료형의 중복을 허용하지 않는 특성을 이용해 풀 수 있다.
testcase = int(input())
for _ in range(testcase):
num_1_cnt = int(input())
num_1 = set(map(int, input().split()))
num_2_cnt = int(input())
num_2 = list(map(int, input().split()))
for i in num_2:
if i in num_1: print(1)
else: print(0)
2번 코드
이진탐색을 이용한 풀이. 이진탐색 코드를 함수로 만들지 않고 for 문안에 직접 넣고 사용하면 시간초과가 난다. 이유는 몰루... 아마 이중 삼중으로 for문이 사용되어 그런 것이 아닐까 생각하고 있다.
import sys
input = sys.stdin.readline
testcase = int(input())
def binary_search(arr, target, start, end):
while(start <= end):
mid = (start+end) // 2
if arr[mid] == target:
return 1
elif arr[mid] > target: end = mid - 1
else: start = mid + 1
return 0
for _ in range(testcase):
num_1_cnt = int(input())
num_1 = sorted(list(map(int, input().split())))
num_2_cnt = int(input())
num_2 = list(map(int, input().split()))
for num2 in num_2:
print(binary_search(num_1, num2, 0, num_1_cnt-1))
'백준' 카테고리의 다른 글
[백준] 2003번 수들의 합2 파이썬 코드 (0) | 2023.09.07 |
---|---|
[백준] 1940번 주몽 파이썬 코드 (0) | 2023.09.07 |
[백준] 1654번 랜선 자르기 파이썬 코드 (0) | 2023.09.06 |
[백준] 2512번 예산 파이썬 코드 (0) | 2023.09.05 |
[백준] 1072번 게임 파이썬 코드 (0) | 2023.09.05 |