이번 문제는 신고 게시판 로직을 구현하는 문제다. 사용자는 여러 사람을 신고할 수 있고 신고 당한 사람이 k번 신고를 당하면 신고한 사람에게 해당 유저를 정지했다는 이메일을 보내야 하는데 그럼 이 때 신고한 사람이 받는 이메일의 갯수를 구하는 문제다.
조건에 같은 사람이 여러 번 신고를 해도 1번 신고한 거와 같이 처리를 해야 한다고 했기에 Object 로 신고한 사람과 신고당한 사람을 관리했다.
function solution(id_list, report, k) {
const idReportMap = {};
id_list.forEach((idListItem, index) => {
idReportMap[idListItem] = {
reported: {},
report: {},
};
});
report.forEach(reportItem => {
const [reportUser, reportedUser] = reportItem.split(' ');
const reportUserCount = idReportMap[reportUser].report[reportedUser];
idReportMap[reportUser].report[reportedUser] = 'report';
idReportMap[reportedUser].reported[reportUser] = 'reported';
});
const result = [];
id_list.forEach(idListItem => {
let emailCount = 0;
Object.keys(idReportMap[idListItem].report)
.forEach(reportedUser => {
if(Object.keys(idReportMap[reportedUser].reported).length >= k) {
emailCount++;
}
});
result.push(emailCount);
});
return result;
}