[BOJ] 10816: 숫자카드2

이슬비·2023년 11월 14일
0

Algorithm

목록 보기
102/110
post-thumbnail

옛날에는 뚝딱뚝딱 잘 풀어냈던 것 같은데 ...

1. 내 풀이: 실패 (시간초과)

# 실버 4 | 숫자 카드 2

import sys
input = sys.stdin.readline

n = int(input())
nlist = list(map(int, input().split()))

m = int(input())
mlist = list(map(int, input().split()))

result = ""
cnt = 0

for i in mlist:
    for j in nlist:
        if i == j:
            cnt += 1
    result += str(cnt) + " "
    cnt = 0
    
print(result)            

이것이 나의 풀이.
사실 시간 초과 나겠다고 생각은 하고 있었다 ㅋ ...
도저히 이 방법 밖에 기억이 나지 않아 푼 방식.

2. 다른 풀이

import sys
input = sys.stdin.readline
from collections import Counter

n = int(input())
nlist = list(map(int, input().split()))

m = int(input())
mlist = list(map(int, input().split()))

c = Counter(nlist)
print(' '.join(f'{c[i]}' if i in C else '0' for i in m))

출처: https://chancoding.tistory.com/45

이 분의 풀이를 보고 내 풀이를 조금 수정했다. 이 분 블로그에 가보면 5가지 방법을 제시하는데, 결국 포인트는 "nlist의 원소의 갯수를 가지고 있는 dictionary를 잘 구해보자." 라는 것이다.

이분탐색 등의 방법을 이용할 수 있지만, 이미 파이썬 내에서 해당 부분에 대한 구현이 모두 이루어져있으므로, 나는 바로 Counter를 사용하였다.

3. 느낀 점

매일 매일 꾸준히 열심히.
작은 것의 힘을 무시하지 말기.

profile
정말 알아?

0개의 댓글