알고리즘/프로그래머스
[Kotlin] 의상
hongeeii
2024. 1. 8. 00:15
728x90
반응형
class Solution {
fun solution(clothes: Array<Array<String>>): Int {
val clothesMap = hashMapOf<String, Int>()
clothes.forEach {
var c = clothesMap.getOrDefault(it[1], 0)
clothesMap[it[1]] = ++c
}
val result = clothesMap.values.fold(1) { total, num ->
total * (num + 1)
}
return result - 1
}
}
좀 더 코틀린스럽게 짜는 연습을 해야겠다..
fun solution(clothes: Array<Array<String>>): Int {
return clothes.groupBy { it[1] }.values.fold(1) { acc, v -> acc * (v.size + 1) } - 1
}
fun solution(clothes: Array<Array<String>>) = clothes
.groupBy { it[1] }.values // group by type of clothes, keep only names of clothes
.map { it.size + 1 } // number of things to wear in a group (including wearing nothing)
.reduce(Int::times) // combine
.minus(1) // remove the case where the spy wears nothing
728x90
반응형