当前位置:网站首页>[leetcode] 1905 statistics sub Island
[leetcode] 1905 statistics sub Island
2022-07-02 15:36:00 【Crisp ~】
Here are two for you m x n The binary matrix of grid1 and grid2 , They only contain 0 ( Water area ) and 1 ( Representing land ). One Islands By Four directions ( Horizontal or vertical ) On the adjacent 1 The composition of the region . Any area outside the matrix is considered a water area .
If grid2 An island of , By grid1 An island of Completely contain , in other words grid2 Every grid in the island is grid1 The same island in the world completely contains , Then we call grid2 This island in China is Sub Islands .
Please return grid2 in Sub Islands Of number .
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
explain : As shown in the figure above , On the left for grid1 , On the right is grid2 .
grid2 The winner of the bid is red 1 The area is a sub island , All in all 3 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
** explain :** As shown in the figure above , On the left for grid1 , On the right is grid2 .
grid2 The winner of the bid is red 1 The area is a sub island , All in all 2 Sub Islands .
Tips :
- m == grid1.length == grid2.length
- n == grid1[i].length == grid2[i].length
- 1 <= m, n <= 500
- grid1[i][j] and grid2[i][j] Either 0 Or 1 .
# Breadth first traversal
class Solution(object):
def countSubIslands(self, grid1, grid2):
m = len(grid2)
n = len(grid2[0])
result = 0# Count the number of sub Islands
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
queue = []
grid2[i][j] = 0
queue.append((i,j))
flag = 1# Whether the flag is a sub Island
while queue:
x,y = queue[0]
queue.pop(0)
if grid1[x][y] == 0:# If in grid1 The corresponding position in is ocean , Then the island is not a sub island
flag = 0
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if 0<=xx<m and 0<=yy<n and grid2[xx][yy]==1:
grid2[xx][yy] = 0
queue.append((xx,yy))
result += flag
return result
# Depth-first traversal
class Solution(object):
def countSubIslands(self, grid1, grid2):
m = len(grid2)
n = len(grid2[0])
result = 0
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
stack = []
grid2[i][j] = 0
stack.append((i,j))
flag = 1
while stack:
x,y = stack[-1]
stack.pop()
if grid1[x][y] == 0:
flag = 0
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:# Traverse in four directions
if 0<=xx<m and 0<=yy<n and grid2[xx][yy]==1:
grid2[xx][yy] = 0
stack.append((xx,yy))
result += flag
return result
边栏推荐
- [leetcode] 1254 - count the number of closed Islands
- 高考分数线爬取
- 【LeetCode】1254-统计封闭岛屿的数量
- Map introduction
- (4) Flink's table API and SQL table schema
- 【网络安全】网络资产收集
- Redux——详解
- 19_ Redis_ Manually configure the host after downtime
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- How to write sensor data into computer database
猜你喜欢

06_ Stack and queue conversion

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

Map introduction

02_ Linear table_ Sequence table

LeetCode刷题——验证二叉树的前序序列化#331#Medium

Case introduction and problem analysis of microservice

Download blender on Alibaba cloud image station

There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant

Leetcode skimming -- sum of two integers 371 medium

SQL stored procedure
随机推荐
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
Steps for Navicat to create a new database
folium地图无法显示的问题,临时性解决方案如下
【Leetcode】167-两数之和II -输入有序数组
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
Leetcode skimming -- sum of two integers 371 medium
Yolo format data set processing (XML to txt)
5. Practice: jctree implements the annotation processor at compile time
19_ Redis_ Manually configure the host after downtime
How does the computer set up speakers to play microphone sound
【LeetCode】876-链表的中间结点
MySQL calculate n-day retention rate
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
[leetcode] 200 number of islands
13_ Redis_ affair
党史纪实主题公益数字文创产品正式上线
15_Redis_Redis.conf详解
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
17_Redis_Redis发布订阅