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

Leetcode Problem 815. Bus Routes

815. Bus Routes

Leetcode Solutions

Breadth-First Search (BFS) with Bus Stops as Nodes

  1. If source equals target, return 0.
  2. Create a map adjList to store bus stops as keys and lists of route indices as values.
  3. Initialize a queue q and a set vis to track visited routes.
  4. Add initial routes to q and mark them in vis.
  5. Perform BFS until q is empty: a. Pop a route from q. b. For each stop in the route: i. If stop equals target, return bus count. ii. For each route in adjList with this stop, add unvisited routes to q and mark visited.
  6. If BFS completes without reaching target, return -1.
UML Thumbnail

Breadth-First Search (BFS) with Routes as Nodes

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...