ntree.ai
ntree.aiLargest Island

Largest Island

mediumsolvedattempted

You are given an m x n matrix grid of 0s and 1s, where 1 is land and 0 is water.

Land cells joined horizontally or vertically form an island.

Return the number of land cells in the largest island, or 0 if there is no land.

Example 1:

1
1
0
0
1
0
0
1
0
0
1
1
0
0
1
0
Input:  grid = [[1, 1, 0, 0],
                [1, 0, 0, 1],
                [0, 0, 1, 1],
                [0, 0, 1, 0]]
Output: 4
Explanation: the island in the bottom-right corner holds four cells.

Constraints:

  • 1 <= the number of rows in grid <= 500
  • 1 <= the number of columns in grid <= 500
  • each cell is 0 or 1
def largestIsland(grid: list[list[int]]) -> int:
pass
 
1
1
0
0
1
0
0
1
0
0
1
1
0
0
1
0

Press Visualise to step through your code.