2023
01.27

출처: 프로그래머스 코딩 테스트 연습
https://school.programmers.co.kr/learn/courses/30/lessons/92334

 

 

 

 

나의 풀이

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

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

        var reportSet = id_list.ToDictionary(e => e, e => new HashSet<string>());
        var reportedSet = id_list.ToDictionary(e => e, e => new HashSet<string>());

        for(int i = 0; i < report.Length; i++)
        {
            string[] split = report[i].Split();
            string reporter = split[0];
            string abuser = split[1];

            reportSet[reporter].Add(abuser);
            reportedSet[abuser].Add(reporter);
        }

        var blockIDs = reportedSet.Where(e => e.Value.Count >= k)
                                  .Select(e => e.Key);

        return id_list.Select(id => reportSet[id].Count(q => blockIDs.Contains(q)))
                      .ToArray();
    }
}

 

다른사람 풀이

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

public class Solution {
    public int[] solution(string[] id_list, string[] report, int k) {
             var tReport = report.Distinct().
                Select(s => s.Split(' ')).
                GroupBy(g => g[1]).
                Where(w => w.Count() >= k).
                SelectMany(sm => sm.Select(s => s[0])).
                ToList();

            return id_list.ToDictionary(x => x, x => tReport.Count(c => x == c)).Values.ToArray();
    }
}

SelectMany는 나중에 좀 다시 봐야할 것 같다.

 

 

COMMENT