diff --git a/Keys and Rooms.java b/Keys and Rooms.java new file mode 100644 index 0000000..4c9ddd8 --- /dev/null +++ b/Keys and Rooms.java @@ -0,0 +1,19 @@ +class Solution { + boolean[] visited; + public boolean canVisitAllRooms(List> rooms) { + visited = new boolean[rooms.size()]; + visited[0] = true; + dfs(rooms, 0); + for(boolean b : visited) if(!b) return false; + return true; + } + + public void dfs(List> rooms, int ind) { + for(int i : rooms.get(ind)) { + if(!visited[i]){ + visited[i] = true; + dfs(rooms, i); + } + } + } +}