알고리즘
[프로그래머스] [C#] 신고 결과 받기
HyoSeong
2022. 2. 27. 11:32
반응형
안녕하세요.
오늘은 프로그래머스 "2022 KAKAO BLIND RECRUITMENT" 문제인 "신고 결과 받기" 를 풀어 보았습니다.
문제 링크
https://programmers.co.kr/learn/courses/30/lessons/92334
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
문제 풀이 방법
Hash를 이용하여 사용자를 신고한 사람별로 묶어서 문제를 풀었습니다.
자세한 풀이는 주석으로 남겨 두었습니다.
코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(string[] id_list, string[] report, int k)
{
// 절대 신고가 불가능한 경우이다.
if(k >= id_list.Length)
{
return new int[id_list.Length];
}
Dictionary<string, int> result = new Dictionary<string, int>();
ReportData[] rDatas = new ReportData[id_list.Length];
for(int i=0; i<id_list.Length; i++)
{
result.Add(id_list[i], 0);
rDatas[i] = new ReportData { Name = id_list[i], ReportPeople = new HashSet<string>() };
}
foreach(string ids in report)
{
string[] id = ids.Split(' ');
// 신고당한 사람의 Hash에 신고한 사람을 추가한다.
// 이렇게 되면 Hash에 의해 중복은 제거되고, 사용자가 몇번 신고되었는지 알 수 있다.
rDatas.First(x => x.Name == id[1]).ReportPeople.Add(id[0]);
}
for(int i=0; i<id_list.Length; i++)
{
// k 번 이상 신고받은 사람만 추려낸다.
if(rDatas[i].ReportCount < k)
{
continue;
}
foreach(var id in rDatas[i].ReportPeople)
{
result[id]++;
}
}
// id_list 순으로 횟수를 return한다.
return result.Select(x => x.Value).ToArray();
}
public class ReportData
{
public string Name { get; set; }
public int ReportCount { get { return ReportPeople.Count; } }
public HashSet<string> ReportPeople { get; set; }
}
}
반응형