Skip to content

Commit

Permalink
Create Keys and Rooms.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivekchavda1374 authored Nov 9, 2024
1 parent 7afb034 commit 4f3efed
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Keys and Rooms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
boolean[] visited;
public boolean canVisitAllRooms(List<List<Integer>> 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<List<Integer>> rooms, int ind) {
for(int i : rooms.get(ind)) {
if(!visited[i]){
visited[i] = true;
dfs(rooms, i);
}
}
}
}

0 comments on commit 4f3efed

Please sign in to comment.