Added leetcode 695
This commit is contained in:
parent
38bf6a3084
commit
cc6a5e28b3
28
leetcode/695_max_area_of_island.py
Normal file
28
leetcode/695_max_area_of_island.py
Normal file
@ -0,0 +1,28 @@
|
||||
class Solution:
|
||||
def maxAreaOfIsland(self, grid: list[list[int]]) -> int:
|
||||
biggest_island = 0
|
||||
counter = 0
|
||||
|
||||
def dfs(row: int, col: int):
|
||||
nonlocal counter
|
||||
if not (0 <= row < len(grid)) or not (0 <= col < len(grid[0])):
|
||||
return
|
||||
|
||||
if grid[row][col] != 1:
|
||||
return
|
||||
|
||||
counter += 1
|
||||
grid[row][col] = 0
|
||||
dfs(row + 1, col)
|
||||
dfs(row - 1, col)
|
||||
dfs(row, col + 1)
|
||||
dfs(row, col - 1)
|
||||
|
||||
for row in range(len(grid)):
|
||||
for col in range(len(grid[0])):
|
||||
if grid[row][col] == 1:
|
||||
dfs(row, col)
|
||||
biggest_island = max(biggest_island, counter)
|
||||
counter = 0
|
||||
|
||||
return biggest_island
|
Loading…
x
Reference in New Issue
Block a user