SW Test/Programmers

[Programmers] Lv2. ์œ„์žฅ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 9. 25. 21:46
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42578

 

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/42578

 

 

 

ํ’€์ด

class Solution {
    fun solution(clothes: Array<Array<String>>): Int = 
    clothes.groupBy { it.last() }   // ์˜์ƒ์˜ ์ข…๋ฅ˜ ๊ทธ๋ฃนํ•‘
        .mapValues { it.value.map { it.first() }}   // ์˜์ƒ์˜ ์ด๋ฆ„๋งŒ ๊ฐ€์ง€๋„๋ก ๋ณ€ํ˜•
        .map { it.value }   // ํ‚ค ๊ฐ’์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฆฌ์ŠคํŠธ
        ?.let { it.fold(1){acc, list -> acc*(list.size + 1) } - 1}  // ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐฏ์ˆ˜ + 1 ์„ ๊ณฑํ•œ ํ›„ - 1
}

 

 

 

์ฐธ๊ณ 

 

mapValues - Kotlin Programming Language

 

kotlinlang.org

 

groupBy - Kotlin Programming Language

 

kotlinlang.org

 

fold - Kotlin Programming Language

 

kotlinlang.org

 

๋ฐ˜์‘ํ˜•