<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code>. Each cell has one of two values:</p>

<ul>
	<li><code>0</code> represents an <strong>empty</strong> cell,</li>
	<li><code>1</code> represents an <strong>obstacle</strong> that may be removed.</li>
</ul>

<p>You can move up, down, left, or right from and to an empty cell.</p>

<p>Return <em>the <strong>minimum</strong> number of <strong>obstacles</strong> to <strong>remove</strong> so you can move from the upper left corner </em><code>(0, 0)</code><em> to the lower right corner </em><code>(m - 1, n - 1)</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio-1.png" style="width: 605px; height: 246px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,1],[1,1,0],[1,1,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).
It can be shown that we need to remove at least 2 obstacles, so we return 2.
Note that there may be other ways to remove 2 obstacles to create a path.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio.png" style="width: 405px; height: 246px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
	<li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> <strong>or</strong> <code>1</code>.</li>
	<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
</ul>

class Solution:
    def minimumObstacles(self, grid: List[List[int]]) -> int:
        """
        Do BFS with 0 obstacles -> if we reach end cell, we return 0
        Do BFS with 1 obstacle removed (explore all possible ways) -> if we reach end cell, we return 1
        ...

        """
        
        """
        #obstacles in grid = K
        minObstaclesRemoved will lie in the range of [0, K]
        midVal = (0 + K) // 2 = K // 2
        TC -> (log K) * (N^K)
        """

        """
        node -> (cell, 0)
        node in Example 1 at (0, 1)
        if I move right, my state node becomes (rightNode, +1)
        if I move left, my state node becomes (leftNode, 0)
        
        """

        # check when grid is 1X1

        ROWS, COLS = len(grid), len(grid[0])
        LAST_CELL = (ROWS - 1, COLS - 1)

    

        minObstacles = defaultdict(lambda: float("inf"))

        queue = [(0, (0, 0))]
        
        
        def getNeighbors(currCell):
            row, col = currCell

            potentialNeighbors = [(row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)]

            result = []
            for newRow, newCol in potentialNeighbors:
                if 0 <= newRow < ROWS and 0 <= newCol < COLS:
                    result.append((newRow, newCol))
            
            return result

        
        def isObstacle(currCell):
            row, col = currCell
            return grid[row][col] == 1
        
        

        while queue:
            obstaclesRemoved, currCell = heapq.heappop(queue)
            
            if currCell == LAST_CELL:
                return obstaclesRemoved

            neighborCells = getNeighbors(currCell)
            for neighborCell in neighborCells:
                if isObstacle(neighborCell):
                    if obstaclesRemoved + 1 < minObstacles[neighborCell]:
                        heapq.heappush(queue, (obstaclesRemoved + 1, neighborCell))
                        minObstacles[neighborCell] = obstaclesRemoved + 1
                else:
                    if obstaclesRemoved < minObstacles[neighborCell]:
                        heapq.heappush(queue, (obstaclesRemoved, neighborCell))
                        minObstacles[neighborCell] = obstaclesRemoved
        
        return minObstacles



3758