나의 풀이
from collections import defaultdict
def solution(id_list, report, k):
count_dict = defaultdict(int)
people_dict = defaultdict(list)
for p in report:
g, r = p.split(' ')
if r not in people_dict[g]:
count_dict[r] += 1
people_dict[g].append(r)
count_list = [p for p in count_dict.keys() if count_dict[p] >= k]
answer = []
for i in id_list:
count = 0
for t in people_dict[i]:
if t in count_list:
count += 1
answer.append(count)
return answer
다른 사람 풀이
def solution(id_list, report, k):
# id만큼의 빈 리스트 생성
answer = [0] * len(id_list)
# defaultdict과 같은 역할
# {'muzi': 0, 'frodo': 0, 'apeach': 0, 'neo': 0}
reports = {x : 0 for x in id_list}
# set으로 중복제거
# 신고 횟수 누적
for r in set(report):
reports[r.split()[1]] += 1
# reports로 신고 당한 id의 횟수 세기
for r in set(report):
if reports[r.split()[1]] >= k:
answer[id_list.index(r.split()[0])] += 1
return answer