bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 505. The Maze II

505. The Maze II

Leetcode Solutions

Approach # Using Dijkstra Algorithm and Priority Queue

  1. Initialize a 2D array distance with infinity values, except for the start position which is set to 0.
  2. Create a priority queue and add the start position along with its distance (0).
  3. While the priority queue is not empty: a. Extract the node with the minimum distance from the queue. b. If this node is the destination, return the distance. c. If this node has been visited, continue to the next iteration. d. Mark the node as visited. e. Explore all possible directions (up, down, left, right) from this node until hitting a wall. f. For each new position reached, if the distance to this new position is less than the current distance, update it and add the new position to the queue.
  4. If the destination was not reached, return -1.
UML Thumbnail

Approach # Depth First Search

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...