https://programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스에 올라온 2022 카카오 블라인드 중 신고 결과 받기 문제를 자바스크립트로 풀었습니다.
전체 유저 목록 id_list,
유저가 유저를 신고한 내용 report,
정지되는 신고건수 k를 받았을때
정지되었다고 받은 메일의 개수 배열을 반환하는 문제입니다.
function solution(id_list, report, k) {
let report_info = {};
let mail_count = {};
for (let command of report) {
const [sender, receiver] = command.split(" ");
if (report_info[receiver] && report_info[receiver].includes(sender))
continue;
report_info[receiver] = [...(report_info[receiver] || []), sender];
}
for (let id of id_list) {
if (report_info[id] && report_info[id].length >= k) {
for (let id2 of report_info[id])
mail_count[id2] = mail_count[id2] ? mail_count[id2] + 1 : 1;
}
}
const answer = id_list.map((id) => mail_count[id] || 0);
return answer;
}
신고 내용 report를 for문으로 돌면서 중복이 안되게끔
{ 신고받은사람 : [신고 한사람] }의 데이터를 만듭니다.
신고한사람이 k이상이면 정지된 사람이고, 이 때 신고한사람들의 mail_count를 1개 올립니다.