-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkeys-and-rooms.py
38 lines (25 loc) · 918 Bytes
/
keys-and-rooms.py
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
import unittest
class Solution:
def canVisitAllRooms(self, rooms):
stack = [0]
rooms_state = [False] * len(rooms)
while stack:
key = stack.pop()
rooms_state[key] = True
for new_key in rooms[key]:
if not rooms_state[new_key]:
stack.append(new_key)
return False not in rooms_state
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_one_room_empty(self):
self.assertTrue(self.sol.canVisitAllRooms([[]]))
def test_one_room(self):
self.assertTrue(self.sol.canVisitAllRooms([[0]]))
def test_custom1(self):
self.assertTrue(self.sol.canVisitAllRooms([[1],[2],[3],[]]))
def test_custom2(self):
self.assertFalse(self.sol.canVisitAllRooms([[1,3],[3,0,1],[2],[0]]))
if __name__ == "__main__":
unittest.main()