ntree.ai

Largest Island

hardMatrixGraphsBFSDFSUnion-Find

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 group an island.

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

Example 1:

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 <= len(grid) <= 500
  • each cell is 0 or 1