프로그래머스 - 의상

이윤주·2023년 8월 13일

코딩테스트

목록 보기
18/18

출처
프로그래머스 - 의상

ex

clothesreturn
[["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)

  1. yellow_hat (headgear)
  2. green_turban (headgear)
  3. blue_sunglasses (eyewear)
  4. yellow_hat (headgear) + blue_sunglasses (eyewear)
  5. green_turban (headgear) + blue_sunglasses (eyewear)
    6 머리장식의 경우 종류1, 종류2, 하나도 안고를 경우 해서 총 3 , 마찬가지로 눈 3, 신발 2 이므로

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
}

1개의 댓글

comment-user-thumbnail
2023년 8월 13일

정보에 감사드립니다.

답글 달기