This repository was archived by the owner on Feb 19, 2020. It is now read-only.
forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeysAndRooms_841.java
More file actions
57 lines (47 loc) · 1.54 KB
/
KeysAndRooms_841.java
File metadata and controls
57 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.leetcode.problems.medium;
import java.util.*;
/**
* @author neeraj on 10/09/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class KeysAndRooms_841 {
public static void main(String[] args) {
solveProblem(new int[][]
{{4}, {3}, {}, {2, 5, 7}, {1}, {}, {8, 9}, {}, {}, {6}}
);
solveProblem(new int[][]{
{1},{2},{3},{}
});
}
private static void solveProblem(int[][] inputs) {
List<List<Integer>> rooms = new ArrayList<>();
List<Integer> keys;
for (int i = 0; i < inputs.length; i++) {
keys = new ArrayList<>();
for (int j = 0; j < inputs[i].length; j++) {
keys.add(inputs[i][j]);
}
rooms.add(keys);
}
System.out.println("Can visit all rooms ? " + canVisitAllRooms(rooms));
}
public static boolean canVisitAllRooms(List<List<Integer>> rooms) {
// To avoid visiting the room already visited.
Set<Integer> visitedRooms = new HashSet<>();
visitedRooms.add(0);
Stack<Integer> dfs = new Stack<>();
dfs.add(0);
Integer polledKey;
while (!dfs.isEmpty()) {
List<Integer> polledKeys = rooms.get(dfs.pop());
for(Integer key: polledKeys) {
if(!visitedRooms.contains(key)) {
visitedRooms.add(key);
dfs.add(key);
}
}
}
return visitedRooms.size() == rooms.size();
}
}