We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
There was an error while loading.Please reload this page.
1 parent68ab990 commit443dfcaCopy full SHA for 443dfca
18-01 Martix
@@ -17,4 +17,27 @@ class Solution:
17
q.append((newX, newY))
18
return matrix
19
20
-Go through the solution once again
+Go through the solution once again ✅
21
+Algo1 repeat
22
+
23
+class Solution:
24
+ def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
25
+ q = deque()
26
+ for i in range(len(matrix)):
27
+ for j in range(len(matrix[0])):
28
+ if matrix[i][j] == 0:
29
+ q.append((i, j))
30
+ else:
31
+ matrix[i][j] = '#' # marked as unvisited
32
33
+ while q:
34
+ x, y = q.popleft()
35
+ for r, c in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
36
+ newX, newY = x+r, y+c
37
+ if 0 <= newX < len(matrix) and 0 <= newY < len(matrix[0]) and matrix[newX][newY] == '#':
38
+ matrix[newX][newY] = matrix[x][y] + 1
39
+ q.append((newX, newY))
40
+ return matrix
41
42
43
+# if we run bfs from whenever we incounter 1 then the time complexity will be 1 * O(n * m)