All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0841_keys_and_rooms.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0801_0900.s0841_keys_and_rooms

// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Data_Structure_II_Day_19_Graph
// #Graph_Theory_I_Day_7_Standard_Traversal #2023_03_28_Time_189_ms_(69.23%)_Space_35.5_MB_(97.44%)

import java.util.TreeSet

class Solution {
    fun canVisitAllRooms(rooms: List?>): Boolean {
        val visited: MutableSet = HashSet()
        visited.add(0)
        val treeSet = TreeSet(rooms[0])
        while (treeSet.isNotEmpty()) {
            val key = treeSet.pollFirst()
            if (!visited.add(key)) {
                continue
            }
            if (visited.size == rooms.size) {
                return true
            }
            treeSet.addAll(rooms[key]!!)
        }
        return visited.size == rooms.size
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy