[프로그래머스] 코딩테스트 연습 - 44

krkorklo·2022년 2월 10일
0

프로그래머스

목록 보기
44/78

level 2 - 위장

의상들이 담긴 2차원 배열 clothes가 주어질 때 서로 다른 옷의 조합의 수를 return 하도록 solution 함수를 작성해주세요.

입출력 예시
clothes : [["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]]
-> 5

function solution(clothes) {
    var answer = 1;
    var cloth_map = new Map();
    clothes.forEach((c) => {
        if (c[1] in cloth_map) cloth_map[c[1]].push(c[0]);
        else cloth_map[c[1]] = [c[0]];
    })
    for (var c in cloth_map) {
        answer *= (cloth_map[c].length + 1);
    }
    return answer - 1;
}

0개의 댓글