Leetcode Problem 2045. Second Minimum Time to Reach Destination

2045. Second Minimum Time to Reach Destination

Leetcode Solutions

Breadth First Search (BFS) Approach

  1. Create an adjacency list from the given edges.
  2. Initialize two arrays, dist1 and dist2, to store the minimum and second minimum time to reach each node, respectively.
  3. Use a queue to perform BFS. Start by enqueuing the first node with a frequency of 1.
  4. While the queue is not empty, dequeue a node and iterate over its neighbors.
  5. For each neighbor, calculate the time to reach it considering the traffic signal pattern.
  6. If dist1[neighbor] is not set, update it with the calculated time. Otherwise, try to update dist2[neighbor] if it's not set or if the calculated time is smaller but not equal to dist1[neighbor].
  7. Enqueue the neighbor with the updated time and frequency.
  8. Once the second minimum time for the destination node is found, return it.
UML Thumbnail

Modified 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...