-
Notifications
You must be signed in to change notification settings - Fork 4
/
walls-and-gates.py
92 lines (84 loc) · 2.91 KB
/
walls-and-gates.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 286. Walls and Gates
# 🟠 Medium
#
# https://leetcode.com/problems/walls-and-gates/
#
# Tags: Array - Matrix - Breadth-First Search
import timeit
from collections import deque
from typing import List
# Use a variable for this the value that represents infinity in this
# problems.
INF = 2147483647
# Using breadth first search, we first queue all gates, then iterate over
# the queue adding infinity nodes to it, then BFS using the queue, for
# each position in the queue, check which of its neighbors is accessible
# update its value with the distance traveled from the gate and add it
# to the queue to process its neighbors on the next iteration.
#
# Time complexity: O(n) - We visit each node a max of 2 times.
# Space complexity: O(n) - The queue could grow to the same size as the
# matrix.
class Solution:
def wallsAndGates(self, rooms: List[List[int]]):
q = deque()
# Traverse the matrix adding gates to the queue.
for i in range(len(rooms)):
for j in range(len(rooms[0])):
if rooms[i][j] == 0:
q.append((i, j))
# Define the possible moves.
directions = ((0, 1), (1, 0), (0, -1), (-1, 0))
# Process the queue
while q:
a, b = q.popleft()
for dir in directions:
# Compute the 4 directional positions from the current
# position and check if we need to update them.
i, j = tuple(i + j for i, j in zip((a, b), dir))
if (
0 <= i < len(rooms)
and 0 <= j < len(rooms[0])
and rooms[i][j] == INF
):
rooms[i][j] = rooms[a][b] + 1
q.append((i, j))
return rooms
def test():
executors = [Solution]
tests = [
[[[-1]], [[-1]]],
[[[-1], [0]], [[-1], [0]]],
[[[0, -1], [INF, INF]], [[0, -1], [1, 2]]],
[
[
[INF, -1, 0, INF],
[INF, INF, INF, -1],
[INF, -1, INF, -1],
[0, -1, INF, INF],
],
[
[3, -1, 0, 1],
[2, 2, 1, -1],
[1, -1, 2, -1],
[0, -1, 3, 4],
],
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.wallsAndGates(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()