Skip to content

2045. Second Minimum Time to Reach Destination 👍

  • Time: $O(|V| + |E|)$, where $|V| = n$ and $|E| = |\texttt{edges}|$
  • Space: $O(|V| + |E|)$, where $|V| = n$ and $|E| = |\texttt{edges}|$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
 public:
  int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
    vector<vector<int>> graph(n + 1);
    queue<pair<int, int>> q{{{1, 0}}};
    // minTime[i][0] := 1st min time to reach node i
    // minTime[i][1] := 2nd min time to reach node i
    vector<vector<int>> minTime(n + 1, vector<int>(2, INT_MAX));
    minTime[1][0] = 0;

    for (const auto& e : edges) {
      const int u = e[0];
      const int v = e[1];
      graph[u].push_back(v);
      graph[v].push_back(u);
    }

    while (!q.empty()) {
      const auto [i, prevTime] = q.front();
      q.pop();
      // start from green
      // if numChangeSignal is odd -> now red
      // if numChangeSignal is even -> now green
      const int numChangeSignal = prevTime / change;
      const int waitTime =
          numChangeSignal & 1 ? change - (prevTime % change) : 0;
      const int newTime = prevTime + waitTime + time;
      for (const int j : graph[i])
        if (newTime < minTime[j][0]) {
          minTime[j][0] = newTime;
          q.emplace(j, newTime);
        } else if (minTime[j][0] < newTime && newTime < minTime[j][1]) {
          if (j == n)
            return newTime;
          minTime[j][1] = newTime;
          q.emplace(j, newTime);
        }
    }

    throw;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
  public int secondMinimum(int n, int[][] edges, int time, int change) {
    List<Integer>[] graph = new List[n + 1];
    Queue<int[]> q = new ArrayDeque<>(Arrays.asList(new int[] {1, 0})); // (index, time)
    // minTime[i][0] := 1st min time to reach node i
    // minTime[i][1] := 2nd min time to reach node i
    int[][] minTime = new int[n + 1][2];
    Arrays.stream(minTime).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));
    minTime[1][0] = 0;

    for (int i = 1; i <= n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] e : edges) {
      final int u = e[0];
      final int v = e[1];
      graph[u].add(v);
      graph[v].add(u);
    }

    while (!q.isEmpty()) {
      final int i = q.peek()[0];
      final int prevTime = q.poll()[1];
      // start from green
      // if numChangeSignal is odd -> now red
      // if numChangeSignal is even -> now green
      final int numChangeSignal = prevTime / change;
      final int waitTime = (numChangeSignal & 1) == 1 ? change - (prevTime % change) : 0;
      final int newTime = prevTime + waitTime + time;
      for (final int j : graph[i])
        if (newTime < minTime[j][0]) {
          minTime[j][0] = newTime;
          q.offer(new int[] {j, newTime});
        } else if (minTime[j][0] < newTime && newTime < minTime[j][1]) {
          if (j == n)
            return newTime;
          minTime[j][1] = newTime;
          q.offer(new int[] {j, newTime});
        }
    }

    throw new IllegalArgumentException();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
  def secondMinimum(self, n: int, edges: List[List[int]], time: int, change: int) -> int:
    graph = [[] for _ in range(n + 1)]
    q = deque([(1, 0)])
    # minTime[i][0] := 1st min time to reach node i
    # minTime[i][1] := 2nd min time to reach node i
    minTime = [[math.inf] * 2 for _ in range(n + 1)]
    minTime[1][0] = 0

    for u, v in edges:
      graph[u].append(v)
      graph[v].append(u)

    while q:
      i, prevTime = q.popleft()
      # start from green
      # if numChangeSignal is odd -> now red
      # if numChangeSignal is even -> now gree
      numChangeSignal = prevTime // change
      waitTime = change - (prevTime % change) if numChangeSignal & 1 else 0
      newTime = prevTime + waitTime + time
      for j in graph[i]:
        if newTime < minTime[j][0]:
          minTime[j][0] = newTime
          q.append((j, newTime))
        elif minTime[j][0] < newTime < minTime[j][1]:
          if j == n:
            return newTime
          minTime[j][1] = newTime
          q.append((j, newTime))
Back to top