Algorithm/프로그래머스
프로그래머스 : 위장(Swift)
콩벌레 개발자
2021. 9. 11. 23:43
Dictionary를 이용하는 문제. 처음에는 경우의 수를 생각해 반복문으로 풀으려고 했으나 그렇게 할 필요가 없이 각 종류별 옷의 수량+1을 곱한 수 마지막에 -1을 빼면 된다. Dictionary는 키값이 옷의 종류이고 value가 종류의 개수다.
func solution(_ clothes:[[String]]) -> Int {
var count = 1
var clothDic : [String : Int] = [:]
for item in clothes {
if clothDic.contains(where: { (key: String, value: Int) in
return key == item[1]
}){
clothDic[item[1]] = clothDic[item[1]]!+1
}else{
clothDic[item[1]]=1
}
}
for item in clothDic.values{
print(item)
count = count * (item+1)
}
return count-1
}