본문 바로가기

알고리즘

[Hash Table] 백준 10816 숫자 카드 2

728x90

[Hash Table] 백준 10816 숫자 카드 2


📌 문제

백준 10816 숫자 카드 2

난이도 : 실버 4

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

 

10816번: 숫자 카드 2

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

www.acmicpc.net

 

분류 : Accepted

 

📌 Hash Table(Dictionary)

target in hash_table.keys() // True or False 반환

 

만약 해시테이블 key값에 해당 숫자 있으면 해당 키값의 벨류를 하나 증가시킴

없다면, key값 추가해주고 해당 키값의 벨류를 1로 설정!

 

📄 나의 풀이

import sys
input = sys.stdin.readline

# 상근
N = int(input())
sang = list(map(int, input().split()))
# N = 10
# sang = [6,3,2,10,10,10,-10,-10,7,3]

# targets
M = int(input())
targets = list(map(int, input().split()))
# M = 8
# targets = [10,9,-5,2,3,4,5,-10]

# hash 이용
hash_table = {}

for e in sang:
    if e not in hash_table.keys():
        hash_table[e] = 1
    else:
        hash_table[e] += 1
print(hash_table)

result = []
for target in targets:
    if target in hash_table.keys():
        result.append(hash_table[target])
    else:
        result.append(0)

print(*result, sep=' ')

log(N)의 시간 복잡도를 가진다.

'알고리즘' 카테고리의 다른 글

프로그래머스, 예산  (0) 2023.07.14
백준 8983 사냥꾼  (0) 2023.07.12
백준 2470 두 용액  (0) 2023.06.01
백준 2110 공유기 설치  (0) 2023.05.31
데일리 알고리즘 230525  (0) 2023.05.25