FunctionRacerProblemsRace

Largest Island After One Fill

HardGridsPython, JavaScript, TypeScript, Java, C++

grid is a map of 1 (land) and 0 (water). An island is a group of land cells joined side by side; cells that only touch at a corner are not joined.

You may turn at most one water cell into land. Return the size of the largest island you can end up with.

Example 1

grid [[1,1,0],[0,0,1],[1,0,1]]
returns 5

Filling the top right cell joins the island of 2 on the left with the island of 2 on the right: 2 + 2 + 1.

Example 2

grid [[1,1],[1,1]]
returns 4

There is no water to fill. The island already covers all 4 cells.

Example 3

grid [[0,0],[0,0]]
returns 1

Any filled cell becomes an island of size 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems