Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 893 Bytes

leetcode_841.md

File metadata and controls

35 lines (27 loc) · 893 Bytes

LeetCode Problems

841. Keys and Rooms

class Solution {
    fun canVisitAllRooms(rooms: List<List<Int>>): Boolean {
        // check each room is opened, and keys
        var opened = BooleanArray(rooms.size, { false })
        val queue: Queue<Int> = LinkedList<Int>()
        queue.add(0)
        // traverse ( BFS )
        while (queue.isNotEmpty()) {
            // get key from pocket and open the room
            val key = queue.poll()
            opened[key] = true
            // get keys from opened room.
            rooms[key].forEach { k -> if (!opened[k]) queue.add(k) }
        }
        // if closed room exist, return false
        opened.forEach { if (!it) return false }
        return true
    }
}