当前位置:网站首页>[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
边栏推荐
- SQL transaction
- 13_ Redis_ affair
- 10_ Redis_ geospatial_ command
- LeetCode刷题——去除重复字母#316#Medium
- YOLOV5 代码复现以及搭载服务器运行
- 02. After containerization, you must face golang
- 提前批院校名称
- Download blender on Alibaba cloud image station
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- 让您的HMI更具优势,FET-G2LD-C核心板是个好选择
猜你喜欢
语义分割学习笔记(一)
怎样从微信返回的json字符串中截取某个key的值?
How to avoid 7 common problems in mobile and network availability testing
18_Redis_Redis主从复制&&集群搭建
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
Pytoch saves tensor to Mat file
LeetCode刷题——验证二叉树的前序序列化#331#Medium
【网络安全】网络资产收集
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
. Solution to the problem of Chinese garbled code when net core reads files
随机推荐
12_Redis_Bitmap_命令
Basic knowledge of cryptography
Force deduction solution summary 2029 stone game IX
Download blender on Alibaba cloud image station
PTA 天梯赛习题集 L2-001 城市间紧急救援
List set & UML diagram
yolo格式数据集处理(xml转txt)
工程师评测 | RK3568开发板上手测试
【LeetCode】417-太平洋大西洋水流问题
高考分数线爬取
XML Configuration File
Steps for Navicat to create a new database
Bing.com網站
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
Pytorch 保存tensor到.mat文件
【LeetCode】577-反转字符串中的单词 III
17_ Redis_ Redis publish subscription
MD5加密
How to avoid 7 common problems in mobile and network availability testing
18_Redis_Redis主从复制&&集群搭建