Maximum Damage Patch
Updated
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[][]) → intExamples
Example 1
grid = [[0,0,1,0],[1,1,1,0],[0,1,0,1]]return = 5The green region has 5 cells, so the answer is 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 = 0There are no land cells.
Example 3
grid = [[0,0,1,1,0,1,0,0],[0,1,0,1,0,1,0,0],[1,1,0,1,0,1,0,0],[0,0,0,0,0,0,0,1]]return = 4The green region has 4 cells, so the answer is 4.
There are four islands. The largest has 4 cells: (0,2), (0,3), (1,3) and (2,3). The island (1,1), (2,0), (2,1) has 3 cells, the column (0,5), (1,5), (2,5) has 3 cells, and (3,7) is alone. Cells that touch only at a corner are not connected, so these islands do not join. The answer is 4.
Example 4
grid = [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]]return = 1The green region has 1 cell, so the answer is 1.
The four land cells sit on a diagonal. Each pair touches only at a corner, so every cell is its own island of area 1.
Constraints
1 <= grid.length, grid[i].length <= 300.- Every row has the same length.
grid[i][j]is either0or1.
More Gecko Robotics Online Assessment questions
Similar questions at other companies
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Maximum Area of IslandSkydio · PHONE SCREENSkydio interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Max Area of IslandGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions
- Max Area of IslandBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Maximum Area of IslandSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW