문제 풀기/C#

74. 신고 결과 받기 (Distinct, Dictionary Value를 List로 만들기)

kagan-draca 2025. 2. 24. 16:08

기본 틀 :

using System;

public class Solution {
    public int[] solution(string[] id_list, string[] report, int k) {
        int[] answer = new int[] {};
        return answer;
    }
}

 

 

신고를 당한 유저를 key, 횟수를 value로 하는 딕셔너리와

 

신고를 한 유저를 key, 당한 유저 목록인 List를 value로 하는 딕셔너리를 만들어줬다.

 

그 후, 반복문을 통해 report를 순회하면서 

 

두 딕셔너리에 값을 채워 넣거나 증가 시켜줬다.

 

마지막으로 신고 횟수가 제한 횟수를 초과하는지 비교하고

 

횟수를 초과한 경우 신고한 유저에게 그 사실을 알릴 수 있도록 만들어줬다.

 

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int[] solution(string[] id_list, string[] report, int k) 
    {
        report = report.Distinct().ToArray();
        List<int> list = new List<int>();
        Dictionary<string, int> bans = new Dictionary<string,int>();
        Dictionary<string, List<string>> reporter = new Dictionary<string, List<string>>();
        
        foreach(string id in id_list)
        {
            reporter[id] = new List<string>();
        }
        
        for(int i = 0; i < report.Length; i++)
        {
            string[] names = report[i].Split(' ');
            
            if(!reporter[names[0]].Contains(names[1]))
            {
                reporter[names[0]].Add(names[1]);
                
                if(!bans.ContainsKey(names[1]))
                    bans[names[1]] = 0;
                
                bans[names[1]]++;
            }
        }
        
        foreach (var entry in reporter)
        {
            int count = 0;
            foreach (string reported in entry.Value)
            {
                if (bans[reported] >= k)
                    count++;
                
            }
            list.Add(count);
        }
        
        return list.ToArray();
    }
}