Second Minimum Time to Reach Destination - Problem

๐Ÿšฆ Navigate Through Traffic Lights to Find the Second Fastest Route

Imagine you're navigating through a city where every intersection has traffic lights that change color simultaneously every few minutes. You need to find the second minimum time to travel from intersection 1 to intersection n.

The city is represented as a bi-directional graph where:

  • Each vertex (1 to n) represents an intersection
  • Each edge represents a road that takes exactly time minutes to traverse
  • Every intersection has a traffic signal that alternates between green and red every change minutes
  • All signals start green and change simultaneously

Traffic Rules:

  • ๐ŸŸข You can enter an intersection at any time
  • ๐ŸŸข You can only leave an intersection when the light is green
  • ๐Ÿ”ด If you arrive at an intersection when the light is red, you must wait
  • โš ๏ธ You cannot wait at an intersection if the light is green (you must leave immediately)

Your goal is to find the second minimum time to reach destination n from source 1. The second minimum is defined as the smallest time that is strictly larger than the minimum time.

Input & Output

example_1.py โ€” Basic Graph
$ Input: n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5
โ€บ Output: 13
๐Ÿ’ก Note: The shortest path is 1โ†’4โ†’5 taking 9 minutes. The second shortest path is 1โ†’3โ†’4โ†’5 taking 13 minutes (with potential traffic light delays).
example_2.py โ€” Simple Path
$ Input: n = 2, edges = [[1,2]], time = 3, change = 2
โ€บ Output: 11
๐Ÿ’ก Note: Only one direct path 1โ†’2. The shortest time is 3 minutes. To get the second shortest, we must wait for a red light cycle, giving us 3 + 2 + 3 + 3 = 11 minutes.
example_3.py โ€” Multiple Paths
$ Input: n = 4, edges = [[1,2],[2,3],[1,3],[3,4]], time = 2, change = 4
โ€บ Output: 8
๐Ÿ’ก Note: Shortest path 1โ†’3โ†’4 takes 4 minutes. Second shortest 1โ†’2โ†’3โ†’4 takes 6 minutes. With traffic lights, the second shortest becomes 8 minutes.

Constraints

  • 2 โ‰ค n โ‰ค 104
  • n - 1 โ‰ค edges.length โ‰ค min(2 ร— 104, n ร— (n - 1) / 2)
  • edges[i].length == 2
  • 1 โ‰ค ui, vi โ‰ค n
  • ui != vi
  • No duplicate edges and graph is connected
  • 1 โ‰ค time, change โ‰ค 103

Visualization

Tap to expand
START234ENDTraffic Light TimingGreen: 0-4 minRed: 5-9 minGreen: 10-14 minPath AnalysisPath 1 (Shortest): 1โ†’4โ†’5Time: 6 minutesPath 2 (2nd Shortest): 1โ†’2โ†’4โ†’5Time: 10 minutes (with wait)
Understanding the Visualization
1
Initialize Journey
Start at intersection 1 with all traffic lights green, ready to explore optimal routes
2
Calculate Timing
At each intersection, check if traffic light is green (go) or red (wait for next cycle)
3
Track Best Routes
Maintain the fastest and second-fastest arrival time to each intersection
4
BFS Exploration
Use breadth-first search to systematically explore all possible paths
5
Find Second Best
Return the second minimum time to reach the destination
Key Takeaway
๐ŸŽฏ Key Insight: Use modified BFS to efficiently find the second minimum path while properly handling traffic light constraints by tracking two arrival times per vertex.
Asked in
Google 35 Amazon 28 Meta 22 Apple 18 Microsoft 15
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen