Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-221

Max Area of Island

Problem

Return the largest area of a four-directionally connected group of 1 cells in a grid of 0 and 1.

Run BFS from every unvisited land cell and track the size of each flood fill. Return 0 when there is no land.

Hint

The queue length after a flood fill equals the island area.

Test cases

case 1
in: [[[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…
expected: 6
case 2
in: [[[0,0,0,0]]]
expected: 0
case 3
in: [[[1]]]
expected: 1
case 4
in: [[[1,1],[0,1]]]
expected: 3
case 5
in: [[]]
expected: 0
Starter codePython
def max_area_of_island(grid):
    # Your code here
    pass

5 test cases · run your code against them in the editor