FastPrepMax Area of Island

Max Area of Island

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a rectangular binary matrix grid, return the maximum area of an island.

An island is a maximal group of 1 cells connected horizontally or vertically. Its area is its number of cells. Return 0 when the grid contains no land.

Function

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

Examples

Example 1

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

The center island has five cells; the bottom-right cell is a separate area-1 island.

Example 2

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

The matrix has no land.

Example 3

grid = [[1]]return = 1

The only cell forms an island of area 1.

Constraints

  • 1 <= grid.length, grid[i].length <= 300.
  • Every row has the same length.
  • Every cell is 0 or 1.

More Bloomberg LP problems

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