FastPrepShortest Maze Path With One Block Removal

Shortest Maze Path With One Block Removal

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

maze is a rectangular grid where 0 is free and 1 is blocked. Start at the top-left cell and reach the bottom-right cell using four-directional moves.

You may change at most one blocked cell into a free cell when entering it. Return the minimum number of moves needed, or -1 when the destination cannot be reached. The start and destination cells are free.

Function

shortestPathWithOneRemoval(maze: int[][]) → int

Examples

Example 1

maze = [[0,1],[0,0]]return = 2

Move down and then right without using the removal.

Example 2

maze = [[0,1,0],[1,1,0],[0,0,0]]return = 4

Remove either blocked neighbor of the start, then continue to the destination in four moves.

Example 3

maze = [[0,1,1],[1,1,1],[1,1,0]]return = -1

Every route needs more than one blocked cell to be removed.

Constraints

  • 1 <= maze.length, maze[0].length <= 200.
  • Every row has the same length.
  • Every cell is 0 or 1.
  • maze[0][0] == maze[m - 1][n - 1] == 0.

More Google problems

See Google hiring insights
public int shortestPathWithOneRemoval(int[][] maze) {
    // write your code here
}
maze[[0,1],[0,0]]
expected2
Checking account…