FastPrepLargest Island After One Land Conversion

Largest Island After One Land Conversion

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a rectangular binary grid, you may change at most one 0 into 1. An island is a four-directionally connected group of ones. Return the largest island area obtainable after the optional conversion.

Function

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

Examples

Example 1

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

Converting either zero joins both diagonal land cells through the converted cell.

Example 2

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

Converting the only zero makes the complete grid one island.

Constraints

  • 1 <= grid.length, grid[i].length <= 500
  • grid[i][j] is 0 or 1
  • four-directional adjacency defines an island

More Bloomberg LP problems

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