Skip to content

[Algorithm] 위장 #31

@hwangJi-dev

Description

@hwangJi-dev

💬 문제

[프로그래머스 스쿨 - 온라인 IT 특화 교육 전문 플랫폼](https://school.programmers.co.kr/learn/courses/30/parts/12077)


💬 Idea

  • Dictionary로 옷의 종류별 개수를 저장한 뒤 조합될 수 있는 수를 구하는 수식을 잘 생각하면 쉬웠던 문제였다.
    • 그런데 수로 접근하지 않고, 모든 조합의 경우를 String으로 반환하려 해서 조금 헤매었다.

💬 풀이

func solution(_ clothes:[[String]]) -> Int {
    var clothesDict: [String: Int] = [:]
    
    for cloth in clothes {
        if clothesDict[cloth[1]] != nil {
            clothesDict[cloth[1]]! += 1
        } else {
            clothesDict[cloth[1]] = 1
        }
    }
    
    var result = 1
    for i in clothesDict.values {
        result *= (i+1)
    }
    
    return result - 1
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions