[LeetCode] 841. Keys and Rooms(Kotlin)

0

LeetCode

목록 보기
35/58
post-thumbnail

[LeetCode] 841. Keys and Rooms(Kotlin)

풀이

class Solution {
    private lateinit var rooms: List<List<Int>>
    private lateinit var isVisited: MutableList<Boolean>

    fun canVisitAllRooms(rooms: List<List<Int>>): Boolean {
        this.rooms = rooms

        val n = rooms.size
        isVisited = MutableList(n){false}

        visitRoom(0)
        return !isVisited.contains(false)
    }

    private fun visitRoom(curRoom:Int){
        if(isVisited[curRoom]) return
        isVisited[curRoom] = true

        rooms[curRoom].forEach{ nextRoom ->
            if(!isVisited[nextRoom]) visitRoom(nextRoom)
        }
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글