<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>

class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        """
        1. calculate the size of all existing islands in the grid and store the sizes in a map
        2. iterate through the grid and check all 0's if by converting it to 1 will it increase
         the size of adjacent islands
        """

        island_id_to_size_mapping = defaultdict(int)
        point_to_island_id = dict()
        points_of_interest = set()
        num_islands = 0
        self.m, self.n = len(grid), len(grid[0])
        ans = 1

        for i in range(self.m):
            for j in range(self.n):
                if grid[i][j] and (i, j) not in point_to_island_id:
                    island_size = self. get_island_size(i, j, grid, point_to_island_id, num_islands, points_of_interest)
                    island_id_to_size_mapping[num_islands] = island_size
                    num_islands += 1
                    ans = max(ans, island_size)

        for x, y in points_of_interest:
            seen = {point_to_island_id[(neigh_x, neigh_y)] for neigh_x, neigh_y in self.get_neighbors(x, y) if (neigh_x, neigh_y) in point_to_island_id}
            ans = max(ans, 1 + sum(island_id_to_size_mapping[i] for i in seen))
            
        return ans
    
    def get_neighbors(self, x, y):
        for del_x, del_y in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
            neigh_x = x + del_x
            neigh_y = y + del_y
            if 0 <= neigh_x < self.m and 0 <= neigh_y < self.n:
                yield neigh_x, neigh_y

    def get_island_size(self, i, j, grid, point_to_island_id, island_id, points_of_interest):
        queue = deque([(i, j)])
        point_to_island_id[(i, j)] = island_id
        m, n = len(grid), len(grid[0])
        size = 0
        while queue:
            curr_points_in_queue = len(queue)

            for _ in range(curr_points_in_queue):
                x, y = queue.popleft()
                size += 1

                for neigh_x, neigh_y in self.get_neighbors(x, y):

                    if (neigh_x, neigh_y) not in point_to_island_id and grid[neigh_x][neigh_y]:
                        queue.append((neigh_x, neigh_y))
                        point_to_island_id[(neigh_x, neigh_y)] = island_id
                    if not grid[neigh_x][neigh_y]:
                        points_of_interest.add((neigh_x, neigh_y))

        return size


        
60306