Max Area of Island

Medium#695grid

You get a map of land and water cells. Land cells that touch by a side form one island, and its area is the number of its cells. Find the area of the biggest island, or 0 if there is no land. Our map has 4 rows: # # . #, . . # ., . # # #, # . # . (# is land) — the answer is 5: the plus in the middle; the other islands have 2, 1 and 1 cells.

input
grid: an m × n map, each cell 1 for land or 0 for water.
returns
The area of the biggest island, or 0 if there is no land. An island is land cells joined up, down, left or right, not diagonally; its area is the number of its cells.
constraints
  • m = grid.length, n = grid[i].length, 1 ≤ m, n ≤ 25
  • grid[i][j] is 0 or 1
our 14 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} grid
 * @return {number}
 */
function maxAreaOfIsland(grid) {

}
Ctrl ↵
format
reset

saved in this browser

14 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more depth-first search (dfs) problems

Pattern “Depth-first search (DFS)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.