출처
프로그래머스 - 의상
ex
| clothes | return |
|---|---|
| [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]] | 5 |
| [["crow_mask", "face"], ["blue_sunglasses", "face"], ["smoky_makeup", "face"]] | 3 |
아예 안입는 경우는 x
경우의 수를 생각해 볼 것, 의상종류에 따른 의상 갯수로 경우의 수를 구해야 한다.
=> array당 한 last index가 같은것은 표기되면 xx
예제 1)
x(headgear갯수 2), y(eyewear 갯수), z(face)
(2 + 1) * (1 + 1) - 1 (아예 안입는 경우는 없기 때문에 빼기) = 5
function solution(clothes) {
let wearable = clothes.reduce((acc, curr) => {
acc[curr[1]] = acc[curr[1]] ? acc[curr[1]] + 1 : 1
return acc
},{})
let arr = []
for(let property in wearable) {
arr.push(wearable[property] + 1)
}
return arr.reduce((acc,curr) => acc * curr) - 1
}
정보에 감사드립니다.