프로그래머스 Lv.2: 위장

Steve·2021년 11월 13일
0

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

해쉬를 사용하면 되는 문제로, 나는 JS의 Map() 을 사용해서 풀었다.

다만 경우의 수를 구할 때 조금 헤맸는데, 나중에 다른 사람의 풀이를 보니 단순히 "안입는 경우"를 위해 +1 해주면 되는거였다...

function solution(clothes) {
    var answer = 1;
    let hash = new Map();
    for (let [name, type] of clothes){
        if (!hash.has(type)) hash.set(type, 1);
        else hash.set(type, hash.get(type) + 1);
    }
    let arr = Array.from(hash.values());
    arr = arr.map(e => e + 1) // 입지 않은 경우 추가
    for (let x of arr){
        answer *= x;
    }
    return answer - 1; // 아무것도 안입는 경우 제외
}
profile
게임과 프론트엔드에 관심이 많습니다.

0개의 댓글