본문 바로가기

TIL(today i learned)/프로그래머스-코딩테스트

[Lv.1] 신고결과 받기

프로그래머스 Lv.1 신고결과 받기 문제 도전!

 

1차시도 -> 실패ㅠㅠ

function solution(id_list, report, k){
    var answer = [];
    // id_list[i] 번째 유저의 id를 변수명으로 지정하고, 해당 유저를 신고한 유저의 id를 값으로 push
    for(let i =0; i < id_list.length; i++){
        this[id_list[i]] = [];
        this[id_list[i]+"_report"] = 0;

        for (let j =0; j < report.length; j++){
            let name = report[j].split(" ")[1];
            if (id_list[i] === name){
                this[id_list[i]].push(report[j].split(" ")[0]);
            }
        }
    }

    // id_list[i]번째 유저를 신고한 유저 id의 중복 값 제거
    for(let i = 0; i < id_list.length; i++){
        let new_arr = [];
        if(this[id_list[i]].length >= k){
            new_arr = this[id_list[i]].filter((element, index) => this[id_list[i]].indexOf(element) === index);
        }
        // 신고한 유저별 받을 mail 수 count
        for(const element of new_arr){
            for(const id of id_list){
                if(element === id){
                    this[id+"_report"]++;
                }
            }
        }
    }
    // 유저별 report 수를 result array에 push
    for(const id of id_list){
        answer.push(this[id+"_report"]);
    }
    return answer;
    
}

    

solution(["muzi", "frodo", "apeach", "neo"], ["muzi frodo","apeach frodo", "frodo neo","muzi neo","apeach muzi"], 2);
solution(["con", "ryan"], ["ryan con", "ryan con", "ryan con", "ryan con"], 6);