-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountSubIsland.py
55 lines (43 loc) · 2.22 KB
/
CountSubIsland.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
'''
You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land).
An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.
An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.
Return the number of islands in grid2 that are considered sub-islands.
Example 1:
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
Example 2:
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
Output: 2
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
'''
class Solution:
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
ROWS = len(grid1)
COLS = len(grid1[0])
visit = set()
def dfs(r,c):
if r < 0 or c < 0 or r >= ROWS or c >= COLS or (r,c) in visit or grid2[r][c] ==0:
return True
visit.add((r,c))
res = True
if grid1[r][c] == 0:
res = False
res = dfs(r+1,c) and res
res = dfs(r-1,c) and res
res = dfs(r,c+1) and res
res = dfs(r,c-1) and res
return res
count = 0
for r in range(ROWS):
for c in range(COLS):
if grid2[r][c] == 1 and (r,c) not in visit and dfs(r,c):
count += 1
return count
'''
Runtime: 3384 ms, faster than 69.02% of Python3 online submissions for Count Sub Islands.
Memory Usage: 115.7 MB, less than 22.60% of Python3 online submissions for Count Sub Islands.
'''