1
+ # Runtime: 170 ms (Top 85.91%) | Memory: 15.7 MB (Top 23.94%)
1
2
class Solution :
2
3
def minDays (self , grid : List [List [int ]]) -> int :
3
4
cnt = 0
@@ -6,20 +7,20 @@ def minDays(self, grid: List[List[int]]) -> int:
6
7
if grid [i ][j ]:
7
8
cnt += 1 # count the number of elements
8
9
root = (i , j ) # assign the root node for the graph
9
-
10
- if cnt <= 1 : return cnt # no elements in the map
11
-
10
+
11
+ if cnt <= 1 : return cnt # no elements in the map
12
+
12
13
vis , low , time , res = {root }, {}, {}, []
13
-
14
+
14
15
# find whether articulation points are present in the matrix
15
- def articulation_points (curr , parent ):
16
+ def articulation_points (curr , parent ):
16
17
low [curr ] = time [curr ] = len (vis )
17
18
children = 0
18
19
i , j = curr
19
-
20
+
20
21
for x , y in [(i + 1 , j ), (i - 1 , j ), (i , j + 1 ), (i , j - 1 )]:
21
22
if (x , y ) == parent : continue
22
-
23
+
23
24
if 0 <= x < len (grid ) and 0 <= y < len (grid [0 ]) and grid [x ][y ]:
24
25
if (x , y ) not in vis :
25
26
vis .add ((x ,y ))
@@ -30,7 +31,7 @@ def articulation_points(curr, parent):
30
31
res .append ([i , j ])
31
32
else :
32
33
low [curr ] = min (low [curr ], time [(x , y )])
33
-
34
+
34
35
if parent == (- 1 , - 1 ) and children > 1 :
35
36
res .append ([x , y ])
36
37
@@ -42,4 +43,3 @@ def articulation_points(curr, parent):
42
43
return 1
43
44
else : # worst case, no articulation points
44
45
return 2
45
-
0 commit comments