풀이
/**
* 위장.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 해주면된다.
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스][자바스크립트][Level1][카카오] 다트 게임 (0) | 2022.02.11 |
---|---|
[프로그래머스][자바스크립트][Level1][카카오] 신고 결과 받기 (0) | 2022.02.11 |
[프로그래머스][자바스크립트][Level1] 최소 직사각형 (0) | 2022.01.09 |
[프로그래머스][자바스크립트][Level1] 약수의 개수와 덧셈 (0) | 2022.01.09 |
[프로그래머스][자바스크립트][Level1] 나누어 떨어지는 숫자 배열 (0) | 2022.01.09 |
댓글