Problem · Array

Max Area of Island

Learn this problem
MediumRoku logoRokuFULLTIMEONSITE INTERVIEW

Problem statement

Given a rectangular binary matrix grid, return the maximum area of an island. An island is a maximal group of cells containing 1 connected horizontally or vertically. Its area is its number of cells.

Return 0 when the matrix contains no land.

Function

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

Examples

Example 1

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

The center island contains five orthogonally connected land cells. The bottom-right cell is a separate island of area 1.

Example 2

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

There are no land cells.

Constraints

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

More Roku problems

drafts saved locally
public int maxIslandArea(int[][] grid) {
    // Write your code here.
}
grid[[0,0,1,0],[1,1,1,0],[0,1,0,1]]
expected5
checking account