Advanced Graphs 0-1 BFS
596. Minimum Obstacle Removal to Reach Corner
You are given a grid of integers, grid, with m rows and n columns. A cell that holds 0 is empty, and
a cell that holds 1 has an obstacle in it. You can remove any obstacle, and its cell then becomes empty.
You start in the top-left cell, (0, 0), and want to reach the bottom-right cell, (m - 1, n - 1). Each
step moves to the cell directly above, below, left or right, and it may only land on an empty cell.
Return the smallest number of obstacles you must remove so that a path from the top-left cell to the bottom-right cell exists.
Example 1
Input: grid = [[0, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 1],
[1, 1, 1, 1, 0]]
1Explanation: Removing the obstacle at (2, 4) is enough. The path goes down the left column, right along row 2 to (2, 2), up to the top row, right to (0, 4), and then down the right column through (2, 4).
Example 2
Input: grid = [[0, 1, 1, 0, 1, 0]]
3Explanation: The grid is a single row, so there is only one path, and it passes all three obstacles.
Example 3
Input: grid = [[0, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 0]]
4Explanation: Every cell but the two corners has an obstacle. Every path passes at least 4 cells between the corners, and each of them has to be cleared.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
m == grid.length,n == grid[i].length1 ≤ m, n ≤ 1052 ≤ m · n ≤ 105grid[i][j]is 0 or 1.grid[0][0] == grid[m - 1][n - 1] == 0
Clarifying questions
What you would ask an interviewer before writing any code.