Leetcode Problem 2290. Minimum Obstacle Removal to Reach Corner

2290. Minimum Obstacle Removal to Reach Corner

Leetcode Solutions

- BFS Approach

  1. Initialize a 2D array distance with infinity to store the minimum number of obstacles removed to reach each cell.
  2. Set distance[0][0] to 0 since the starting cell does not require any obstacles to be removed.
  3. Create a deque and add the starting cell (0, 0) to it.
  4. While the deque is not empty, pop the cell from the front of the deque.
  5. If the current cell is the bottom-right corner, return the distance to this cell as the result.
  6. For each of the four possible directions (up, down, left, right), calculate the new cell coordinates.
  7. If the new cell is within bounds and has not been visited, update its distance if the current path offers a lower number of obstacles removed.
  8. Depending on whether the new cell is an obstacle or not, add it to the front or back of the deque.
  9. Continue this process until the deque is empty or the bottom-right corner is reached.
  10. Return the distance to the bottom-right corner as the minimum number of obstacles to remove.
UML Thumbnail

Dijkstra's Algorithm Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...