当前位置:网站首页>[leetcode] 695 - maximum area of the island
[leetcode] 695 - maximum area of the island
2022-07-02 15:36:00 【Crisp ~】
Give you a size of m x n The binary matrix of grid .
Islands It's made up of some neighboring 1 ( Representing land ) A combination of components , there 「 adjacent 」 Ask for two 1 Must be in In four horizontal or vertical directions adjacent . You can assume grid The four edges of are all 0( Representative water ) Surrounded by a .
The area of the island is the value of 1 Number of cells .
Calculate and return grid The largest island area in . If there were no Islands , Then the return area is 0 .
Example 1:
Input :
grid = [
[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output : 6
explain : The answer should not be 11 , Because the island can only contain horizontal or vertical 1 .
Example 2:
Input : grid = [[0,0,0,0,0,0,0,0]]
Output : 0
Tips :
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 50
- grid[i][j] by 0 or 1
# Breadth first search
class Solution(object):
def maxAreaOfIsland(self, grid):
n = len(grid)# Sequence length
m = len(grid[0])# Sequence width
max_S = 0# Record the maximum area
queue = []# queue
for i in range(n):
for j in range(m):
if grid[i][j]==1:# When you meet land, you start to search all the land adjacent to it
S=1
grid[i][j]=0
queue.append((i,j))
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<n and 0<=yy<m and grid[xx][yy]==1:
S+=1
grid[xx][yy]=0
queue.append((xx,yy))
max_S=max(max_S,S)
return max_S
class Solution(object):
def maxAreaOfIsland(self, grid):
n = len(grid)
m = len(grid[0])
max_S = 0
stack = []
for i in range(n):
for j in range(m):
if grid[i][j]==1:
S=1
grid[i][j]=0
stack.append((i,j))
while stack:
x,y = stack[-1]
stack.pop()
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<n and 0<=yy<m and grid[xx][yy]==1:
S+=1
grid[xx][yy]=0
stack.append((xx,yy))
max_S=max(max_S,S)
return max_S
边栏推荐
- Build your own semantic segmentation platform deeplabv3+
- 5. Practice: jctree implements the annotation processor at compile time
- Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
- 【LeetCode】876-链表的中间结点
- YOLOV5 代码复现以及搭载服务器运行
- Redux - detailed explanation
- LeetCode刷题——奇偶链表#328#Medium
- 10_ Redis_ geospatial_ command
- 4. Jctree related knowledge learning
- Redux——详解
猜你喜欢
15_ Redis_ Redis. Conf detailed explanation
怎样从微信返回的json字符串中截取某个key的值?
Redux - detailed explanation
14_ Redis_ Optimistic lock
03_ Linear table_ Linked list
Markdown tutorial
5. Practice: jctree implements the annotation processor at compile time
Leetcode skimming -- count the number of numbers with different numbers 357 medium
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
自定义异常
随机推荐
Tidb data migration tool overview
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
folium地图无法显示的问题,临时性解决方案如下
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
【LeetCode】200-岛屿数量
. Net again! Happy 20th birthday
MD5加密
Markdown tutorial
Pytoch saves tensor to Mat file
Common English abbreviations for data analysis (I)
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
【LeetCode】1020-飞地的数量
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
夏季高考文化成绩一分一段表
你不知道的Set集合
16_Redis_Redis持久化
SQL transaction
21_ Redis_ Analysis of redis cache penetration and avalanche
Solution of Queen n problem