<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>

<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1],[1,0]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1],[1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

from collections import defaultdict

class Solution:

    # Could find areas of all the islands
    # Then see if we can connect any by going through 0s and trying to flip to 1 and seeing adjacencies, then take the max

    def __init__(self):
        self.visited = set()

    def helper(self, grid, i, j, island_pixels):  # island_pixels modified in place (which gives area)
        self.visited.add((i, j))
        island_pixels.append((i, j))

        def _visitable(ii, jj):
            return grid[ii][jj] and (ii, jj) not in self.visited

        if i + 1 < len(grid) and _visitable(i+1, j):
            self.helper(grid, i + 1, j, island_pixels)
        if i - 1 >= 0 and _visitable(i-1, j):
            self.helper(grid, i - 1, j,island_pixels)
        if j + 1 < len(grid[i]) and _visitable(i,j+1):
            self.helper(grid, i, j + 1, island_pixels)
        if j - 1 >= 0 and _visitable(i,j-1):
            self.helper(grid, i, j - 1, island_pixels)

    def largestIsland(self, grid: List[List[int]]) -> int:
        unique_vals = set()
        for row in grid:
            unique_vals.update(row)
        if len(unique_vals) == 1:
            if list(unique_vals)[0] == 1:
                return len(grid) * len(grid[0])
            else:
                return 1

        # flood fill each island and determine its size
        # then we could have a grid where each pixel is the size of the island it belongs to
        coord_to_iid = defaultdict(lambda: None)
        iid_to_area = defaultdict(int)
        iid = 0
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j] == 1 and (i, j) not in self.visited:
                    island_pixels = []
                    self.helper(grid, i, j, island_pixels)
                    iid_to_area[iid] = len(island_pixels)
                    for coord in island_pixels:
                        coord_to_iid[coord] = iid
                    iid += 1
        # Now, we want to go through the 0s and see what could happen
        best_so_far = 0
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j] == 0:
                    # See what happens if we flip it to 1
                    adjacent_islands = set([
                        coord_to_iid[(i - 1, j)],
                        coord_to_iid[(i + 1, j)],
                        coord_to_iid[(i, j - 1)],
                        coord_to_iid[(i, j + 1)]
                    ]) - {None}
                    new_area = 1 + sum(iid_to_area[iid] for iid in adjacent_islands)
                    best_so_far = max(new_area, best_so_far)
        return best_so_far

        
75981