TIL #37

loci·2024년 6월 6일
0

TIL

목록 보기
35/103

신고결과 받기

해당 유저가 신고당한 횟수 k를 초과하면 해당 유저를 신고한 유저들이 처리결과 메일을 받게 되는데 이 처리결과 메일을 받은 횟수를 배열로 반환해줘야 한다, 단 중복된 신고는 처리하지 않는다.

  1. 중복된 신고를 방지하기위해 collection set으로 변환해 중복값을 제거해주었다.
  2. map을 만들어 report를 순회해 각 유저가 신고당한 횟수를 체크해주었다.
  3. 신고당한 횟수가 k번을 넘어간 유저들을 filter 해주었다.
  4. filter된 유저들(k번이상 신고된)을 신고한 유저들을 카운트 해주었다.
  5. 카운트된 숫자들을 answer로 넘겨주었다.

나의코드

class Solution {
    fun solution(id_list: Array<String>, report: Array<String>, k: Int): IntArray {
        var answer: IntArray = intArrayOf()
        
        var setReport = report.toSet()
        var mapId = mutableMapOf<String, Int>()
        
        for(i in id_list){
            mapId[i] = 0
        }
        var mapResult = mapId.toMutableMap()
        
        for (i in setReport){
            val key = i.split(" ")[1]
            if(key in mapId){
                mapId[key] = mapId[key]!! + 1
            }
        }
        
        val filteredKey = mapId.filter{it.value >= k}
        
        for(i in setReport){
            val reporter = i.split(" ")[0]
            val reported = i.split(" ")[1]
            
            if(reported in filteredKey){
                mapResult[reporter] = mapResult[reporter]!! + 1
            }
        }
        
        
        for(i in mapResult){
            answer += i.value
        }
        
        return answer
    }
}

처음에 거의 모든 문제에서 틀리게 나왔는데 k를 2로 잘못넣어주고 setReport를 report로 잘못넣은 거 였어서 이부분을 바꿔 해결해줌.

또 몇몇문제에서 "출력크기초과"오류가 났는데 println()을 제거해주지 않아 생긴 문제였음


다른사람 풀이

class Solution {
    fun solution(id_list: Array<String>, report: Array<String>, k: Int): IntArray =
    report.map { it.split(" ") }
        .groupBy { it[1] }
        .asSequence()
        .map { it.value.distinct() }
        .filter { it.size >= k }
        .flatten()
        .map { it[0] }
        .groupingBy { it }
        .eachCount()
        .run { id_list.map { getOrDefault(it, 0) }.toIntArray() }
}

기억할 점

array를 set으로 간단하게 만들어줄 수 있다.

var setReport = report.toSet()

map을 copy해야할때 toMutableMap을 이용

var mapResult = mapId.toMutableMap()

map에 값을 넣어줄때 key값이 null이면 안되므로 !!나 null이들어가지않는 방법을 추가해주어야함

mapId[key] = mapId[key]!! + 1
profile
편리한 개발자

0개의 댓글