반응형
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
알고리즘 분류
- 자료 구조
- 정렬
- 이분 탐색
- 해시를 사용한 집합과 맵
SOLUTION
해시를 이용한 풀이
import sys
N = int(sys.stdin.readline())
card = list(map(int, sys.stdin.readline().split()))
M = int(sys.stdin.readline())
card_cnt = list(map(int, sys.stdin.readline().split()))
hashmap = {} # 해시를 이용한 풀이
for n in card:
if n in hashmap: # 이미 카운트 된 수일 경우
hashmap[n] += 1
else: # 처음 카운트 될 경우
hashmap[n] = 1
for m in card_cnt:
if m in hashmap:
print(hashmap[m], end = " ")
else:
print(0, end = " ")
카운터 함수를 이용한 풀이
from collections import Counter
import sys
N = int(sys.stdin.readline())
card = list(map(int, sys.stdin.readline().split()))
M= int(sys.stdin.readline())
card_cnt = list(map(int, sys.stdin.readline().split()))
card = Counter(card) # Counter 함수를 통해 각각의 숫자가 몇 개씩 있는지 세준다
print(card)
for i in card_cnt:
print(card[i], end = " ")
'코딩테스트 대비 > BOJ' 카테고리의 다른 글
[Baekjoon/Python] 10845번: 큐 - 효과는 굉장했다! (0) | 2021.10.30 |
---|---|
[Baekjoon/Python] 10828번: 스택 - 효과는 굉장했다! (0) | 2021.10.30 |
[Baekjoon/Python] 10773번: 제로 - 효과는 굉장했다! (0) | 2021.10.30 |
[Baekjoon/Python] 9012번: 괄호 - 효과는 굉장했다! (0) | 2021.10.29 |
[Baekjoon/Python] 2108번: 통계학 - 효과는 굉장했다! (0) | 2021.10.26 |