
접근
이분탐색 알고리즘을 이용하여 풀이를 하였다. 이분탐색 알고리즘은 오름차순으로 정렬된 리스트의 중간값과 찾고자 하는 값을 비교하여 중간값보다 찾고자 하는 값이 클 경우 중간값부터 끝 값까지, 찾고자 하는 값이 더 작을 경우 첫 값부터 중간값까지의 리스트를 가지고 다시 중간값 탐색을 하는 알고리즘이다.
코드
import sys
def binarysearch(array, start, end, target):
    if start > end:
        return 0
    mid = (start + end) // 2
    if target == array[mid]:
        return 1
    elif target < array[mid]:
        return binarysearch(array, start, mid - 1, target)
    else:
        return binarysearch(array, mid + 1, end, target)
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline())
b = list(map(int, sys.stdin.readline().split()))
a.sort()
for i in b:
    print(binarysearch(a, 0, len(a) - 1, i))더 생각해 볼 것?
...
코드나 내용 관련 조언, 부족한 점 및 질문 언제든 환영합니다!
반응형
    
    
    
  '코딩 > 백준 (Python)' 카테고리의 다른 글
| 백준 10816번: 숫자 카드 2 -dictionary 이용 (Python) (0) | 2021.04.05 | 
|---|---|
| 백준 10816번: 숫자 카드 2 -이분탐색 (Python) (0) | 2021.04.05 | 
| 백준 2261번: 가장 가까운 두 점 (Python, PyPy3) (0) | 2021.04.04 | 
| 백준 6549번: 히스토그램에서 가장 큰 직사각형 - 분할 정복 (Python) (0) | 2021.04.03 | 
| 백준 6549번: 히스토그램에서 가장 큰 직사각형 - 스택 (Python) (0) | 2021.04.03 | 




최근댓글