Max Area of Island
- Difficulty: Medium
- Type: DFS/BFS
- link
Problem
Solution
- Search island using DFS algo
- Keep track of island size
- Return the largest island size
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def dfs(i,j,size):
if i < 0 or j<0 or i >= len(grid) or j >= len(grid[0])or grid[i][j] != 1:
return size
grid[i][j] = 0
size += 1
size = dfs(i,j+1,size)
size = dfs(i,j-1,size)
size = dfs(i+1,j,size)
size = dfs(i-1,j,size)
return size
max_size = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
max_size = max(max_size,dfs(i,j,0))
return max_size