Second Minimum Time to Reach Destination - Problem

A city is represented as a bi-directional connected graph with n vertices where each vertex is labeled from 1 to n (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.

The time taken to traverse any edge is time minutes. Each vertex has a traffic signal which changes its color from green to red and vice versa every change minutes. All signals change at the same time. You can enter a vertex at any time, but can leave a vertex only when the signal is green. You cannot wait at a vertex if the signal is green.

The second minimum value is defined as the smallest value strictly larger than the minimum value. For example, the second minimum value of [2, 3, 4] is 3, and the second minimum value of [2, 2, 4] is 4.

Given n, edges, time, and change, return the second minimum time it will take to go from vertex 1 to vertex n.

Note: You can go through any vertex any number of times, including 1 and n. You can assume that when the journey starts, all signals have just turned green.

Input & Output

Example 1 — Basic Graph
$ Input: n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5
Output: 13
💡 Note: First minimum path: 1→4→5 takes 6 time units. Second minimum: 1→3→4→5 or 1→2→1→4→5 takes 13 time units considering traffic light delays.
Example 2 — Simple Path
$ Input: n = 2, edges = [[1,2]], time = 3, change = 2
Output: 11
💡 Note: Only one direct path 1→2. First minimum is 3. To get second minimum, go 1→2→1→2, which takes 11 time units with traffic delays.
Example 3 — Multiple Routes
$ Input: n = 4, edges = [[1,2],[1,3],[2,4],[3,4]], time = 2, change = 4
Output: 6
💡 Note: Two paths: 1→2→4 and 1→3→4. Both take 4 time units (first minimum). Second minimum requires one detour, taking 6 time units.

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
  • There are no duplicate edges
  • The graph is connected
  • 1 ≤ time, change ≤ 103

Visualization

Tap to expand
Second Minimum Time to Reach DestinationInput Graph1234time = 3, change = 5Green LightPath AnalysisPath 1: 1→3→4Time: 6 (minimum)Path 2: 1→2→1→3→4Time: 13 (2nd min)Includes traffic delaysTraffic Light LogicCycle = current_time ÷ changeEven cycle → Green lightOdd cycle → Red light (wait)Red: wait until next greenGreen: can immediately leaveFind the second minimum time from vertex 1 to vertex nconsidering bidirectional edges and traffic light timingOutput: 13 (second minimum time)
Understanding the Visualization
1
Graph Input
City represented as bidirectional graph with traffic lights
2
Path Finding
Find shortest and second shortest times considering light delays
3
Result
Return second minimum time from vertex 1 to vertex n
Key Takeaway
🎯 Key Insight: Use modified BFS to track both shortest and second shortest times while handling traffic light delays
Asked in
Google 12 Facebook 8 Amazon 6
23.5K Views
Medium Frequency
~35 min Avg. Time
890 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