[프로그래머스] 신고 결과 받기Lv.1

나의 풀이

def solution(id_list, report, k):
    dict_report = {name: [] for name in id_list}
    dict_count = {name: 0 for name in id_list}
    stop = []
    result = []

    for i in report:
        user, get_report_user = i.split()
        if get_report_user not in dict_report[user]:
            dict_report[user].append(get_report_user)

    for i in id_list:
        for j in dict_report:
            if i in dict_report[j]:
                dict_count[i] += 1
        if dict_count[i] >= k:
            stop.append(i)

    for i in dict_report:
        count = 0
        for j in dict_report[i]:
            if j in stop:
                count += 1
        result.append(count)

    return result
  • dict_report는 유저가 신고한 유저들을 담고, dict_count는 유저가 몇 번 신고당했는지 기록하기 위해 사전 자료형으로 생성하였다.
  • stop과 result 는 각각 정지 당한 유저를 담고, 결과 값을 리턴하기 위한 리스트이다.
  • 입력받은 report를 돌면서 dict_report를 채워 줄 것이다. 단 한 유저가 같은 유저를 중복해서 신고하는 것은 1건으로 치기 때문에 중복되지 않게 하기 위해서 if문을 작성하였다.
  • 그리고 유저들의 신고 당한 횟수를 기록하기 위한 반복문을 작성하였다. 만일 해당 유저의 신고 당한 횟수가 k 이상이면 stop 리스트에 해당 유저를 담아준다.
  • 다시 dict_report를 돌면서 해당 유저가 신고한 유저가 정지 당한 유저 리스트에 있다면 카운트를 세고, 카운트를 result 리스트에 담아준다.
  • 반복이 끝나고 해당 result 리스트를 리턴해주면 된다.

느낀점

for문이 너무 많다. 그래서 몇몇 테스트 코드에서 시간 복잡도가 급증했던 것 같다. 더 줄일 수 있을 것 같은데 머리가 안돌아간다.

0개의 댓글