当前位置:网站首页>[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
边栏推荐
- FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
- 17_ Redis_ Redis publish subscription
- Real estate market trend outlook in 2022
- 20_ Redis_ Sentinel mode
- LeetCode刷题——去除重复字母#316#Medium
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- QML pop-up frame, customizable
- 【LeetCode】417-太平洋大西洋水流问题
- 10_Redis_geospatial_命令
- 14_Redis_乐观锁
猜你喜欢
随机推荐
19_ Redis_ Manually configure the host after downtime
MySQL -- Index Optimization -- order by
How does the computer set up speakers to play microphone sound
07_ Hash
MD5加密
14_ Redis_ Optimistic lock
SQL transaction
folium地图无法显示的问题,临时性解决方案如下
04.进入云原生后的企业级应用构建的一些思考
21_Redis_浅析Redis缓存穿透和雪崩
04_ 栈
19_Redis_宕机后手动配置主机
17_ Redis_ Redis publish subscription
你不知道的Set集合
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
03_线性表_链表
夏季高考文化成绩一分一段表
Download blender on Alibaba cloud image station
Data analysis thinking analysis methods and business knowledge - business indicators
自定义异常