 All Problems
Making a Large Island
hard
union-find
graph
matrix
google
amazon

You are given an n x n binary matrix grid. You are allowed to change at most one 0 to 1.

Return the size of the largest island after applying this operation. An island is a group of 1s connected 4-directionally.

Example 1:

Input: grid = [[1,0],[0,1]]
Output: 3

Example 2:

Input: grid = [[1,1],[1,0]]
Output: 4

Constraints:

  • n == grid.length == grid[i].length
  • 1 <= n <= 500
Run to check your code against the sample cases, or submit to run every case