#2290 Minimum Obstacle Removal to Reach Corner
frequently reported atGoogle
the problem
In plain words
grid has 0 (empty) and 1 (obstacle). Move up/down/left/right from (0, 0) to (m − 1, n − 1). Return the minimum number of obstacles you must remove.
in grid = [[0,1,1],[1,1,0],[1,1,0]]
out 2
in grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
out 0
A clear path already exists.
- 1 ≤ m, n ≤ 10⁵
- 2 ≤ m·n ≤ 10⁵
- grid[0][0] == grid[m−1][n−1] == 0
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
Stepping onto an obstacle costs 1, onto an empty cell 0. Minimum total cost with 0/1 weights → 0-1 BFS.
solutions
From brute force to optimal
Edge weight = value of the cell you step onto. Standard heap Dijkstra.
- dist[0][0] = 0.
- Poll, skip stale, relax 4 neighbours with weight grid[nr][nc].
- Return dist of the corner.
4mn relaxations with heap pushes.
dist grid and heap.
class Solution { private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public int minimumObstacles(int[][] grid) { int rows = grid.length, cols = grid[0].length; int[][] dist = new int[rows][cols]; for (int[] row : dist) Arrays.fill(row, Integer.MAX_VALUE); dist[0][0] = 0; PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0])); pq.offer(new int[] {0, 0, 0}); while (!pq.isEmpty()) { int[] top = pq.poll(); int d = top[0], r = top[1], c = top[2]; if (d > dist[r][c]) continue; for (int[] dir : DIRS) { int nr = r + dir[0], nc = c + dir[1]; if (nr < 0 || nc < 0 || nr >= rows || nc >= cols) continue; int nd = d + grid[nr][nc]; // stepping onto an obstacle costs 1 if (nd < dist[nr][nc]) { dist[nr][nc] = nd; pq.offer(new int[] {nd, nr, nc}); } } } return dist[rows - 1][cols - 1]; }}Empty neighbours go to the deque's front, obstacles to the back. The front is always the cheapest.
- dist[0][0] = 0, deque = [(0,0)].
- pollFirst; for each neighbour that improves: w = 0 → offerFirst, w = 1 → offerLast.
- Return dist of the corner.
Each cell is pushed at most twice; constant work per edge.
dist grid and deque.
class Solution { private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public int minimumObstacles(int[][] grid) { int rows = grid.length, cols = grid[0].length; int[][] dist = new int[rows][cols]; for (int[] row : dist) Arrays.fill(row, Integer.MAX_VALUE); dist[0][0] = 0; Deque<int[]> deque = new ArrayDeque<>(); deque.offerFirst(new int[] {0, 0}); while (!deque.isEmpty()) { int[] cur = deque.pollFirst(); int r = cur[0], c = cur[1]; for (int[] dir : DIRS) { int nr = r + dir[0], nc = c + dir[1]; if (nr < 0 || nc < 0 || nr >= rows || nc >= cols) continue; int w = grid[nr][nc]; // 0 = free cell, 1 = obstacle if (dist[r][c] + w < dist[nr][nc]) { dist[nr][nc] = dist[r][c] + w; if (w == 0) deque.offerFirst(new int[] {nr, nc}); else deque.offerLast(new int[] {nr, nc}); } } } return dist[rows - 1][cols - 1]; }}Dry run
in the room
How to say it in the interview
talk track
- Translate: "removing an obstacle = paying 1 to enter that cell."
- Name 0-1 BFS and explain why the deque stays sorted.
traps
- Plain BFS counting steps — it minimises path length, not removals.
- m·n up to 10⁵ with m or n up to 10⁵: allocate by rows × cols, not 10⁵ × 10⁵.
what they ask next
Follow-ups
You may remove at most k obstacles; shortest path length?
That's LC 1293: BFS over states (r, c, removalsLeft).
before you submit
Edge cases to test
- A clear path exists → 0, however long it is.
- A single row: the answer is the number of 1s.
- m or n can be 10⁵ (but m·n ≤ 10⁵): allocate rows × cols, never 10⁵ × 10⁵.
practise next
Similar problems
- #1368Minimum Cost to Make at Least One Valid Path in a GridHardin Shortest Paths
- #54201 MatrixMediumin Graphs
- #1091Shortest Path in Binary MatrixMediumin Graphs