PS/문제 풀이
[프로그래머스/Swift] 42578번 - 의상
시르베어
2025. 1. 26. 23:44
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42578
풀이
- 조합으로 해결가능하다
- 옷 종류에 따른 개수 + 해당 옷을 선택하지 않는 경우로 계산하면 된다
- 최소 하나의 옷을 골라야하기 때문에 전체 경우의 수 - 1(아무것도 선택하지 않는 경우)로 계산한다
전체코드
import Foundation
func solution(_ clothes:[[String]]) -> Int {
var dics: [String:[String]] = [:]
for clothe in clothes { //종류별 정리
if dics[clothe[1]] == nil {
dics[clothe[1]] = []
}
dics[clothe[1]]!.append(clothe[0])
}
var result: Int = 1
for dic in dics {
result *= (dic.value.count+1)
}
return result-1
}