알고리즘/프로그래머스

[프로그래머스][자바스크립트][Level2] 위장

Benjamin_Choi 2022. 1. 10. 21:39

풀이

/**
 * 위장.js
 * Level2
 * https://programmers.co.kr/learn/courses/30/lessons/42578?language=javascript
 */

function solution(clothes) {
    const clothesMap = {};

    clothes.forEach((cloth) => {
        if (!clothesMap[cloth[1]]) {
            clothesMap[cloth[1]] = 1;
        } else {
            clothesMap[cloth[1]]++;
        }
    });

    return Object.keys(clothesMap).reduce((answer, key) => {
        answer *= clothesMap[key] + 1;
        return answer;
    }, 1) - 1;
}

 

옷의 조합을 구하는 문제로, clotheMap 에 조합별 옷의 개수를 넣어주고 해당 옷을 입지 않는 경우까지 +1 해서 answer 에 곱해주고 모든 옷을 입지 않은 경우 1을 빼서 return 해주면된다.