KotlinAlgorithm#20 (POG 겹치는 선분의 길이)

박채빈·2023년 7월 19일
0

KotlinAlgorithm

목록 보기
20/28
post-thumbnail

POG 겹치는 선분의 길이

링크

코드

class Solution {
    fun solution(lines: Array<IntArray>): Int {
        val flat = lines.flatMap { it.toList() }
        val totalLine = (flat.min()..flat.max()).associateWith { 0 }.toMutableMap()
//        val totalLine = (flat.minOf {it}..flat.maxOf {it}).associateWith { 0 }.toMutableMap() // 제출할땐 이렇게 해야 에러 안뜸

        lines.forEach { line -> (line.first()+1..line.last()).forEach { totalLine[it] = totalLine[it]!! + 1 } }

        return totalLine.count { it.value >= 2 }
    }
}

시작 지점을 빼고 계산하는게 포인트

profile
안드로이드 개발자

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

덕분에 좋은 정보 얻어갑니다, 감사합니다.

답글 달기