当前位置:网站首页>[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
边栏推荐
- Wechat Alipay account system and payment interface business process
- JVM architecture, classloader, parental delegation mechanism
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- 夏季高考文化成绩一分一段表
- How to intercept the value of a key from the JSON string returned by wechat?
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
- 4. Data splitting of Flink real-time project
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- 高考分数线爬取
- 11_Redis_Hyperloglog_命令
猜你喜欢

How to intercept the value of a key from the JSON string returned by wechat?

LeetCode刷题——去除重复字母#316#Medium

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)

Wechat Alipay account system and payment interface business process

Pytorch 保存tensor到.mat文件

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

YOLOV5 代码复现以及搭载服务器运行

08_ strand
随机推荐
5. Practice: jctree implements the annotation processor at compile time
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
【LeetCode】344-反转字符串
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
Leetcode question brushing - parity linked list 328 medium
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
21_ Redis_ Analysis of redis cache penetration and avalanche
SQL transaction
[solution] educational codeforces round 82
14_Redis_乐观锁
【LeetCode】1905-统计子岛屿
Engineer evaluation | rk3568 development board hands-on test
【LeetCode】977-有序数组的平方
Custom exception
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
Beijing rental data analysis
Loss function and positive and negative sample allocation: Yolo series
Force deduction solution summary 2029 stone game IX
[leetcode] 189 rotation array
06_ Stack and queue conversion