반응형
1. 문제 소개
- 이분탐색 문제에 해당한다.
2. 코드
import sys
input = sys.stdin.readline
def set_test_case():
N = int(input())
input_str = input().split()
cards = [int(x) for x in input_str]
M = int(input())
input_str = input().split()
your_cards = [int(x) for x in input_str]
return cards , your_cards
def binary_search(arr,search_value):
start = 0
end = len(arr)
while(start<end):
mid = (start + end) // 2
if arr[mid] == search_value:
return mid
elif arr[mid] < search_value:
start = mid+1
else:
end = mid
return -1
def solution():
cards,your_cards = set_test_case()
cache = dict()
for i in cards:
if i not in cache:
cache[i] = 1
else:
cache[i] +=1
sorted_card = sorted(cards)
answer = []
for i in your_cards:
if binary_search(sorted_card,i) > -1:
answer += [str(cache[i])]
else:
answer += [str(0)]
print(" ".join(answer))
solution()
3. 코멘트
- 알고리즘을 정리하면 숫자카드를 자료구조 map 에 저장하고, 상근이의 카드를 이분탐색으로 조회하되, 결과는 map 에서 출력한다.
- 육아로 알고리즘 스터디를 진행할 시간과 여유가 없어 아쉽다. 여유가 곧 생길수 있기를..
반응형
'Algorithm' 카테고리의 다른 글
Baekjoon 백준 알고리즘 - 부분합( 1806 ) (0) | 2023.06.05 |
---|---|
Baekjoon 백준 알고리즘 - 이친수( 2193 ) (0) | 2023.02.18 |
Baekjoon 백준 알고리즘 - 좋은 단어 ( 3986 ) (0) | 2022.08.28 |
Baekjoon 백준 알고리즘 - 시간 관리 ( 1263 ) (0) | 2022.05.15 |
Baekjoon 백준 알고리즘 - 삼각형 만들기 (0) | 2022.05.01 |