Python [Programmers] - 신고 결과 받기

sh_awtylol4·2022년 7월 26일
0

📝 Solution 1

from collections import defaultdict


def solution(id_list, report, k):
    answer = []
    # 중복 신고 제거
    report = list(set(report))
    # user별 신고한 id 저장
    user = defaultdict(set)
    # 신고자가 신고한 id 추가
    cnt  = defaultdict(int)

    for r in report:
        # report의 첫번째 값은 신고자id, 두번째 값은 신고당한 id
        a,b = r.split()
        # 신고자가 신고한 id 추가
        user[a].add(b)
        # 신고당한 id의 신고 횟수 추가
        cnt[b] += 1

    for i in id_list:
        result = 0

        for u in user[i]:
            if cnt[u] >= k:
                result += 1
        answer.append(result)
    return answer

📝 Solution 2

def solution(id_list, report, k):
    answer = [0] * len(id_list)    
    reports = {x : 0 for x in id_list}

    for r in set(report):
        reports[r.split()[1]] += 1

    for r in set(report):
        if reports[r.split()[1]] >= k:
            answer[id_list.index(r.split()[0])] += 1

    return answer
profile
포기하는 그 순간이 바로 시합종료예요...

0개의 댓글