-
기둥과 보 설치하기 (with Kotlin)Programmers Practice/level 3 2025. 7. 3. 22:13
https://school.programmers.co.kr/learn/courses/30/lessons/60061
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
설치된 기둥이 담긴 sticks와 설치된 보가 담긴 bos를 map 처리하여 출력할 수 있도록 해준다. 그리고 그 map 처리한 결과를 '+' 연산자를 통해서 합친 뒤 정렬하여 array로 변환한 뒤 리턴한다.
data class Data( val x: Int, val y: Int ) class Solution { val sticks = mutableSetOf<Data>() val bos = mutableSetOf<Data>() private fun checkBo(x: Int, y: Int): Boolean { return (Data(x, y - 1) in sticks) || (Data(x + 1, y - 1) in sticks) || (Data(x - 1, y) in bos && Data(x + 1, y) in bos) } private fun checkStick(x: Int, y: Int): Boolean { return (y == 0) || (Data(x, y - 1) in sticks) || (Data(x, y) in bos) || (Data(x - 1, y) in bos) } fun check(): Boolean { for ((x, y) in sticks) { if (!checkStick(x, y)) return false } for ((x, y) in bos) { if (!checkBo(x, y)) return false } return true } fun solution(n: Int, build_frame: Array<IntArray>): Array<IntArray> { for (build in build_frame) { val (x, y, a, b) = build val data = Data(x, y) if (b == 0) { if (a == 0) { sticks.remove(data) if (!check()) sticks.add(data) } else { bos.remove(data) if (!check()) bos.add(data) } } else { if (a == 0) { sticks.add(data) if (!check()) sticks.remove(data) } else { bos.add(data) if (!check()) bos.remove(data) } } } return (sticks.map { intArrayOf(it.x, it.y, 0) } + bos.map { intArrayOf(it.x, it.y, 1)}) .sortedWith(compareBy({ it[0] }, { it[1] }, { it[2] })) .toTypedArray() } }'Programmers Practice > level 3' 카테고리의 다른 글
상담원 인원 (with Kotlin) (3) 2025.07.09 110 옮기기 (with Kotlin) (0) 2025.07.09 사라지는 발판 (with Kotlin) (0) 2025.07.03 선입 선출 스케줄링 (with Kotlin) (0) 2025.07.01 풍선 터트리기 (with Kotlin) (0) 2025.06.26