Algorithm
프로그래머스
Python
Receiving Report Results

신고 결과 받기

set을 사용해서 중복을 제거하고 시간 복잡도를 낮췄어야 했는데 그러지 못해서 좀 더 코드가 복잡해졌다.

내 풀이

def solution(id_list, report, k):
    user_hash, count_hash = {}, {}
    answer = []
    
    for i in id_list :
        user_hash[i] = []
        count_hash[i] = 0
    for r in report :
        fromu, tou = r.split(" ")
        if fromu not in user_hash[tou] :
            user_hash[tou].append(fromu)
    for i in id_list :
        if len(user_hash[i]) >= k :
            for u in user_hash[i] :
                count_hash[u] += 1
    for i in id_list :
        answer.append(count_hash[i])
 
    return answer

다른 사람 풀이

def solution(id_list, reports, k):
    stop = []
    answer = [0] * len(id_list)
    dicReports = {id: [] for id in id_list}
    for i in set(reports):
        report = i.split(' ')
        stop.append(report[1])
        dicReports[report[0]].append(report[1])
        
    stop = set([i for i in stop if stop.count(i) >= k])
 
    for key, value in dicReports.items():
        for s in stop:
            if s in value:
                answer[id_list.index(key)] += 1
    return answer

Reference

https://zest1923.tistory.com/65 (opens in a new tab)