아이공의 AI 공부 도전기

[Baekjoon] 10815번 : 숫자 카드 (Python, 정렬)

 

     

 

 

https://www.acmicpc.net/problem/10815

 

10815번: 숫자 카드

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

 

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

시간 초과 - 방법 1 - 코드 길이 195B

 

n = int(input())
s = list(map(int, input().split()))
m = int(input())
r = list(map(int, input().split()))

for i in r:
    if i in s:
        print(1, end=' ')
    else:
        print(0, end=' ')

 

방법 2 - 메모리 111408KB / 시간 2778ms / 코드 길이 424B

 

이분 탐색

 

n = int(input())
s = list(map(int, input().split()))
s.sort()
m = int(input())
r = list(map(int, input().split()))

def binary_search(start,end,target):
    while start <= end:
        mid = (start+end)//2
        if s[mid] == target:
            return 1
        elif s[mid] > target:
            end = mid - 1
        else:
            start = mid + 1
    return 0

for i in r:
    print(binary_search(0, n-1, i), end=' ')

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading