Kotlin/코딩테스트

[프로그래머스] 안전지대 - kotlin

깨노비 2023. 3. 10. 21:03
728x90
반응형

 

class Solution {
    
    fun solution(board: Array<IntArray>): Int {
        var answer: Int = 0

        for (i in 0 until board.size) {
            for (j in 0 until board[i].size) {
                if (board[i][j] == 1) {
                    pointWrite(board, i, j)
                }
            }
        }

        board.forEach { ints -> answer += ints.filter { i -> i == 0 }.count() }
        
        return answer
    }

    fun pointWrite(board: Array<IntArray>, row: Int, col: Int) {
        val fromRow = if(row-1 < 0) row else row-1
        val toRow = if(row+1 >= board.size) row else row+1
        val fromCol = if(col-1 < 0) col else col-1
        val toCol = if(col+1 >= board.size) col else col+1

        for (i in fromRow..toRow) {
            for (j in fromCol..toCol) {
                if (board[i][j] == 0) {
                    board[i][j] = -1
                }
            }
        }
    }
}
728x90
반응형