-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeysAndRooms.java
37 lines (31 loc) · 900 Bytes
/
KeysAndRooms.java
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
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.List;
import java.util.Stack;
// https://leetcode.com/problems/keys-and-rooms/
public class KeysAndRooms {
private final List<List<Integer>> input;
public KeysAndRooms(List<List<Integer>> input) {
this.input = input;
}
public boolean solution() {
boolean[] seen = new boolean[input.size()];
seen[0] = true;
Stack<Integer> stack = new Stack<>();
stack.push(0);
while (!stack.isEmpty()) {
int node = stack.pop();
for (int next : input.get(node)) {
if (!seen[next]) {
seen[next] = true;
stack.push(next);
}
}
}
for (boolean s : seen) {
if (!s) {
return false;
}
}
return true;
}
}