FastPrepMaximum Area of an Island

Maximum Area of an Island

Google logoGoogle● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given a rectangular binary grid, an island is a maximal group of 1 cells connected vertically or horizontally.

Return the largest island area, measured as its number of cells. Return 0 when the grid contains no land. Do not modify the input grid.

Function

maxAreaOfIsland(grid: int[][]) → int

Examples

Example 1

grid = [[0,0,1,0],[1,1,1,0],[0,1,0,0]]return = 5

The central island contains five connected land cells.

Example 2

grid = [[0,0],[0,0]]return = 0

There is no land.

Example 3

grid = [[1,0,1],[0,1,0],[1,0,1]]return = 1

Diagonal cells are not connected, so every island has area one.

Constraints

  • 1 <= rows, columns <= 500.
  • Every cell is 0 or 1.

More Google problems

See Google hiring insights
public int maxAreaOfIsland(int[][] grid) {
    // Write your code here.
}
grid[[0,0,1,0],[1,1,1,0],[0,1,0,0]]
expected5
Checking account…