codejump
EasyPythonJavaScriptTypeScript

Graph — Max Area of Island

Data StructuresGraphsDFSMatrix

solve(grid) — grid contains 1 (land) and 0 (water). An island is a
group of land cells connected 4-directionally. Return the area (number of
cells) of the largest island, or 0 if there is none.

This is the sibling of graph-number-of-islands: same traversal, but the
traversal returns a size instead of just marking cells.

Examples

solve([[0,1,1],[1,1,0],[0,0,1]])          → 4
solve([[0,0,0]])                          → 0
solve([[1,0,1],[1,0,1],[1,1,1]])          → 7   // the 7 cells wrap around the water

Sample tests

Test #1Two islands, the larger has 4 cells
Input: [[[0,1,1],[1,1,0],[0,0,1]]]
Output: 4
Test #2No land
Input: [[[0,0,0]]]
Output: 0
Test #3Single land cell
Input: [[[1]]]
Output: 1