Algorithm

SwiftUI: 의상

songmoro 2024. 4. 2. 09:57
728x90

프로그래머스 의상

 

주어진 옷과 부위 배열에서 도출 가능한 경우의 수를 구하는 문제

 

아무 것도 입지 않는 경우를 제외하고 아래 주석과 같은 형태가 되어서 n * m * … 으로 구현

 

func solution(_ clothes:[[String]]) -> Int {
    clothes.reduce(into: [:], { $0[$1.last!, default: 0] += 1 }).values.reduce(into: 1, { $0 *= ($1 + 1) }) - 1
}

//    clothes    return
//    [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]    5
//    [["crow_mask", "face"], ["blue_sunglasses", "face"], ["smoky_makeup", "face"]]    3

// n = (1), m = (3)
// (0) (0)
// (0) (1)
// (0) (2)
// (0) (3)
// (1) (0)
// (1) (1)
// (1) (2)
// (1) (3)

// n = 3
// (0)
// (1)
// (2)
// (3)
728x90