본문 바로가기
코딩테스트

[1일1코👨🏻‍💻] Lv.2 위장[해시].js - 프로그래머스

by Jacob93 2020. 9. 11.

2020.09.10

출처 : 프로그래머스

📝문제

https://programmers.co.kr/learn/courses/30/lessons/42578

✏️해답

function solution(clothes) {
    var answer = 1;
    var count = 0;
    var result=[];
    var result1=[];

    for(var i=0;i<clothes.length;i++){
        console.log(clothes[i][1]);
        if(result.indexOf(clothes[i][1])==-1){
            result1.push(0);
            result.push(clothes[i][1]);

        }
    }
    console.log(result);
    console.log(result1);

    for(var i=0;i<clothes.length;i++){
        if(result.indexOf(clothes[i][1])!=-1){
            ++result1[result.indexOf(clothes[i][1])];
        }
    }
    console.log(result1);
    for(var i=0;i<result1.length;i++){
        answer *= result1[i]+1;
    }
    answer= answer-1;

    return answer;
}

회고🧐

아직도 map을 써야할지, 배열을 써야할지 잘 모르겠다. map으로 해볼려고 map함수를 찾아봤는데, 다 잘되다가 map은 key값을 통해 value값을 가져오기 때문에 key값을 가져올 수 없다는 걸 발견 하고서 다시 배열로 갈아탔다. [참고] 는 map 함수 관련 자료

function solution(clothes) {
    var answer = 1;
    var newClothes = new Map();
    var kindsOf = new Map();
    var count = 0;
    var result=[];
    var result1=[];
    for(var i=0;i<clothes.length;i++){
        newClothes.set(clothes[i][0],clothes[i][1]);    
    }
    console.log(newClothes.get(clothes[1][0]));
    console.log(newClothes.size);

    for(var i=0;i<newClothes.size;i++){
        console.log(newClothes.get(clothes[i][0]));
        if(!kindsOf.has(newClothes.get(clothes[i][0]))){
            kindsOf.set(newClothes.get(clothes[i][0]),++count);
        }else{
            console.log('---'+kindsOf.get(newClothes.get(clothes[i][0]))+'---');
            // kindsOf.set(kindsOf.get(newClothes.get(clothes[i][0])));
        }
        console.log(newClothes.get(clothes[i][0]));
        kindsOf.has(newClothes.get(clothes[i][1]))
        kindsOf.has(newClothes.get(clothes[i][0])) ? null : kindsOf.set(newClothes.get(clothes[i][0]),++count);
        if(!kindsOf.has(newClothes.get(clothes[i][1]))){
            console.log(i+'번째'+count);
            count++;
            kindsOf.set(newClothes.get(clothes[i][0]),count);
        }
        else{
            kindsOf.set(newClothes.get(clothes[i][1]))
        }
    }
    console.log(kindsOf.get(newClothes.get(clothes[1][0])));

    for(var i=0;i<kindsOf.size;i++){
        console.log(kindsOf.get(newClothes.get(clothes[i][0])));
        result.push(kindsOf.get(newClothes.get(clothes[i][0])));
    }
    console.log(result);
    for(var i=0;i<result.length;i++){
        answer = answer * (result[i]);
    }
    console.log(kindsOf.get(newClothes.get(clothes[0][0])));
    return answer;
}

댓글