티스토리 뷰

https://www.hanbit.co.kr/store/books/look.php?p_code=B8945183661

 

이것이 취업을 위한 코딩 테스트다 with 파이썬

IT 취준생이라면 누구나 가고 싶어 하는 카카오, 라인, 삼성전자의 2016년부터 2020년까지의 코딩 테스트와 알고리즘 대회의 기출문제를 엄선하여 수록하였다.

www.hanbit.co.kr

 

[197p 부품 찾기]

 

import sys

n = int(sys.stdin.readline().rstrip())
stores = list(map(int, sys.stdin.readline().rstrip().split()))[:n]
m = int(sys.stdin.readline().rstrip())
parts = list(map(int, sys.stdin.readline().rstrip().split()))[:m]

# 이진 탐색을 위한 정렬
stores.sort()


# 이진 탐색
def binary_search(array, target, start, end):
    if start > end:
        return

    mid = (start + end) // 2

    # 값을 찾았다면
    if array[mid] == target:
        return mid
    # 찾고자 하는 값이 현재 값보다 작으면
    elif array[mid] > target:
        return binary_search(array, target, start, mid - 1)
    # 찾고자 하는 값이 현재 값보다 크면
    else:
        return binary_search(array, target, mid + 1, end)


for part in parts:
    if binary_search(stores, part, 0, n - 1):
        print('yes', end=" ")
    else:
        print('no', end=" ")

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함