[프로그래머스]신고 결과 받기 게임 (javascript/자바스크립트)

스카치·2023년 3월 25일
0

문제

풀이 1

function solution(id_list, report, k) {
    // report 재설계
    report = report.map(v => {
        return v.split(' ')
    })
    
    // 유저 목록 객체 생성
    let userList = {}
    for (let id of id_list){
        userList[id] = []
    }
    let userListclone = () => JSON.parse(JSON.stringify(userList))
    
    // 1. 유저별 신고한 리스트 목록 생성
    let userReportingList = userListclone()
    report.map(list => {
        userReportingList[list[0]].push(list[1])
        userReportingList[list[0]] = [...new Set(userReportingList[list[0]])]
    })

    // 2. 유저별 신고당한 횟수 객체 만들기 
    let userReportedList = userListclone()
    Object.values(userReportingList).map(reports => {
        reports.map(reported => {
            userReportedList[reported].push(reported)
        })
    })
    
    // 3. 정지된 유저 리스트 만들기
    let penaltyList = []
    for (let [key,value] of Object.entries(userReportedList)){
        if(value.length >= k) penaltyList.push(key)
    }
    
    // 4. email리스트
    let emailList = new Array(id_list.length).fill(0)
    for (let i=0; i<id_list.length; i++) {
        penaltyList.map(reported => {
            if (userReportingList[id_list[i]].includes(reported)) emailList[i]++
        })
    }
    return emailList
 
}
}

풀이 2

function solution(id_list, report, k) {
    let reports = [...new Set(report)].map(a=>{return a.split(' ')});
    let counts = new Map();
    for (const bad of reports){
        counts.set(bad[1],counts.get(bad[1])+1||1)
    }
    let good = new Map();
    for(const report of reports){
        if(counts.get(report[1])>=k){
            good.set(report[0],good.get(report[0])+1||1)
        }
    }
    let answer = id_list.map(a=>good.get(a)||0)
    return answer;
}

풀이 3

function solution(id_list, report, k) {
    report = [...new Set(report)];
    const reported = report.map(el => el.split(' ')[1]);
    const reported_final = [];
    const count = new Array(id_list.length).fill(0);
    reported.forEach(el => {
        count[id_list.indexOf(el)]++;
    });
    count.forEach((el, i) => {
        if(el >= k) {
            reported_final.push(id_list[i])
        }
    });
    count.fill(0);
    report.forEach((el) => {
        el = el.split(' ');
        if(reported_final.includes(el[1])) {
            count[id_list.indexOf(el[0])]++;
        }
    })

    return count;
}

풀이 4

function solution(id_list, report, k) {
    let userId = Array.from(Array(id_list.length), () => 0)
    let target = [];
    let result = {};
    new Set(report).forEach(f => {
        const r = f.split(' ');
        const to = r[1];
        target.push(r);
        result[to] = (result[to] || 0) + 1;
    })
    Object.keys(result).forEach(f => {
        if (result[f] >= k) {
            target.filter(a => a[1] == f).forEach(m => userId[id_list.indexOf(m[0])]++)
        }
    })

    return userId;
}

0개의 댓글