PS (Problem Solving)/Programmers

[프로그래머스] 신고 결과 받기 - 자바스크립트

캐럿노트 2024. 11. 10. 23:54

문제

https://school.programmers.co.kr/learn/courses/30/lessons/92334?language=javascript

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

자바스크립트

function solution(id_list, report, k) {

    // 신고 횟수 누적
    let hash = {}
    for (const receive of report) {
        const receiveList = receive.split(' ')
        if (hash[receiveList[1]]) {
            hash[receiveList[1]].push(receiveList[0])
        } else {
            hash[receiveList[1]] = [receiveList[0]]
        }
    }
    
    // 개인 메일함 생성 및 초기화
    let answerHash = {}
    for (const id of id_list) {
        answerHash[id] = 0
    }
    
    // k번 이상 신고 누적시 신고자에게 메일 전송
    for (const id of id_list) {
        const reportList = [...new Set(hash[id])]
        if (reportList.length >= k){
            for (const toMail of reportList) {
                answerHash[toMail] += 1
            }
        } 
    }
    
    // 정답 출력
    let answer = [];
    for (const id of id_list) {
        answer.push(answerHash[id])
    }
    return answer;
}