[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)
}
}
}