프로그래머스/lv.1
신고 결과 받기
bingual
2024. 1. 31. 02:39
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제풀이
해시를 사용하지 않을 경우 신고 처리를 하는 로직에서 시간 복잡도는 O(n^2) 이상으로 올라가게 되어 시간 초과가 날거다.
from collections import defaultdict
def solution(id_list, report, k):
# 신고/정지 명단
users = {id: {"target": set(), "banned_cnt": 0} for id in id_list}
report_cnt = defaultdict(int) # 신고당한 횟수
# 신고 처리
for item in report:
reporter, target = item.split()
# 명단에 피신고자가 없을 경우 추가하고 신고당한 횟수 카운트
if target not in users[reporter]["target"]:
users[reporter]["target"].add(target)
report_cnt[target] += 1
# 처리 결과 메일통보
for user, cnt in report_cnt.items():
if cnt >= k:
# 정지 처리한 피신고자인 만큼 카운트
for reporter, target in users.items():
if user in target["target"]:
users[reporter]["banned_cnt"] += 1
# 각 유저가 처리 결과 메일을 받은 횟수 반환
answer = [user["banned_cnt"] for user in users.values()]
return answer