Programmers/Lv. 0 (完)

[Kotlin] Programmers 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열

chattymin 2023. 6. 24. 11:37
728x90
반응형

숨어있는 숫자의 덧셈 (2)

class Solution {
    fun solution(my_string: String) = my_string.replace("[^0-9]".toRegex(), " ").split(" ").filter { it != "" }.sumOf { it.toInt() }
}

 

 

 

안전지대

class Solution {
    fun solution(board: Array<IntArray>): Int {
        var room = board
        val size1 = board.get(0).size
        val size2 = board.size

        for (i in 0 until  size2){
            for (j in 0 until   size1){
                if (board[i][j] != 1) continue
                val left = j - 1
                val right = j + 1
                val up = i + 1
                val down = i - 1

                if (check(left, size1)) if (board[i][left] == 0)board[i][left] = 2
                if (check(right, size1)) if (board[i][right] == 0)board[i][right] = 2
                if (check(up, size2)) if (board[up][j] == 0)board[up][j] = 2
                if (check(down, size2)) if (board[down][j] == 0)board[down][j] = 2
                if (check(left, size1) && check(up, size2)) if (board[up][left] == 0)board[up][left] = 2
                if (check(left, size1) && check(down, size2)) if (board[down][left] == 0)board[down][left] = 2
                if (check(right, size1) && check(up, size2)) if (board[up][right] == 0)board[up][right] = 2
                if (check(right, size1) && check(down, size2)) if (board[down][right] == 0)board[down][right] = 2

            }
        }

        var answer: Int = room.map { it.filter {num-> num == 0 }.count() }.sum()


        return answer
    }

    fun check(target: Int, len: Int): Boolean = (target >= 0 && target < len)

}

 

 

 

삼각형의 완성조건 (2)

class Solution {
    fun solution(sides: IntArray): Int = ((sides.maxOrNull()!! - sides.minOrNull()!!)..(sides.maxOrNull()!! + sides.minOrNull()!!) - 2).count()
}

 

 

 

외계어 사전

class Solution {
    fun solution(spell: Array<String>, dic: Array<String>): Int {
        for (i in dic){
            var cnt = 0
            for(j in spell) {
                if (i.contains(j)) cnt++
                if (cnt == spell.size) return 1
            }
        }
        return 2
    }
}
728x90
반응형