https://programmers.co.kr/learn/courses/30/lessons/42578
dictionary.forEach { key, myvalue in
categories += myvalue.count
values *= myvalue.count
}
result = categories + values
if dictionary.count == 1 {
result -= (dictionary.first?.value.count)!
처음에는 이렇게 작성했다.
key의 value를 가지고 주먹구구 식으로 곱하고 빼고 하면 답이 나올 줄고 알고 했지만 하면서도 맞나 싶었고, 굉장히 오래걸리고, 테스트 케이스 정확도도 28.6% 정도 였...
let count = dictionary.mapValues{$0.count}.values
return count.reduce(1){$0*($1+1)} - 1
요 두 줄로 해결 되고, 엄청 빠르고 다 맞았었다.
고차함수에 대한 부분이 많이 부족하다.
고차함수를 풀어서 한줄 한줄 써보고 이해하고 싶다.
func solution(_ clothes:[[String]]) -> Int {
var dictionary: [String:[String]] = [:]
for i in 0..<clothes.count {
if dictionary[clothes[i][1]] != nil {
dictionary[clothes[i][1]]?.append(clothes[i][0])
}
else {
dictionary[clothes[i][1]] = [clothes[i][0]]
}
}
let count = dictionary.mapValues{$0.count}.values
return count.reduce(1){$0*($1+1)} - 1
}