승자와 패자 각각 해시맵에 넣고,
승자엔 있는데 패자엔 없으면 무패배열에,
패자에서 1인것들은 1패배열에 넣어서 리턴
참고:전체배열에서 특정배열안에 있는걸 제거할땐 리스트를 쓰면 remove가 o(n)인데,hashSet같은 hash를 사용하면 o(1)임
정답
class Solution {
fun findWinners(matches: Array<IntArray>): List<List<Int>> {
val winnerHashMap = hashMapOf<Int, Int>()
val loserHashMap = hashMapOf<Int, Int>()
for (i in matches){
winnerHashMap[i[0]]= winnerHashMap[i[0]]?.plus(1)?:1
loserHashMap[i[1]]= loserHashMap[i[1]]?.plus(1)?:1
}
val perfectList= winnerHashMap.keys.toHashSet()
for (i in loserHashMap.keys){
perfectList.remove(i)
}
val oneLoseList= mutableListOf<Int>()
for ((key,value) in loserHashMap){
if (value==1){
oneLoseList.add(key)
}
}
println(winnerHashMap)
println(loserHashMap)
println(perfectList)
return listOf<List<Int>>(perfectList.sorted(),oneLoseList.sorted())
}
}
'알고리즘' 카테고리의 다른 글
릿코드 1207. Unique Number of Occurrences 코틀린 (0) | 2024.01.17 |
---|---|
릿코드 380. Insert Delete GetRandom O(1) 코틀린 (0) | 2024.01.16 |
릿코드 1657. Determine if Two Strings Are Close 코틀린 (1) | 2024.01.14 |
릿코드 1347. Minimum Number of Steps to Make Two Strings Anagram 코틀린 (0) | 2024.01.13 |
릿코드 1704. Determine if String Halves Are Alike 코틀린 (0) | 2024.01.12 |