We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8bac4aa commit 075404cCopy full SHA for 075404c
number-of-islands/sun912.py
@@ -0,0 +1,26 @@
1
+"""
2
+ TC: O(m*n)
3
+ SC: O(m*n)
4
5
+class Solution:
6
+ def numIslands(self, grid: List[List[str]]) -> int:
7
+ def dfs(i, j):
8
+ if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == "-1" or grid[i][j] == "0":
9
+ return
10
+
11
+ grid[i][j] = "-1"
12
+ dfs(i+1, j)
13
+ dfs(i, j+1)
14
+ dfs(i-1, j)
15
+ dfs(i, j-1)
16
17
+ if not grid:
18
+ return 0
19
20
+ count = 0
21
+ for i in range(len(grid)):
22
+ for j in range(len(grid[0])):
23
+ if grid[i][j] == "1":
24
+ count += 1
25
+ dfs(i, j)
26
+ return count
0 commit comments