Second Minimum Time to Reach Destination
Problem (restated)
An undirected unweighted graph on nodes 1…n. Traversing any edge takes time minutes. Each node has a traffic light: change minutes green, then change minutes red, starting green at t=0. You may leave a node only while green (wait if you arrive on red). Return the second-minimum arrival time at n from 1.
Intuition
You need two distinct arrival times at n, so each node stores a first and a second time (strictly larger). Dijkstra/BFS keyed by arrival. Before leaving, if ⌊t / change⌋ is odd, wait until the next green: t += change - t % change. Then every neighbor arrives at t + time.
Approaches
First and second arrival
UnverifiedIdea. dist1[v], dist2[v]. On a candidate nt: if nt < dist1[v], shift first down to second and set first; else if dist1[v] < nt < dist2[v], set second. Push in both cases. The first time you pop n with t > dist1[n], that t is the answer. Equal times are ignored (not “second”).
Walkthrough. n=2, one edge, time=3, change=2. First arrival at 2 is 3. Leave 2 after waiting to 4, back to 1 at 7, wait to 8, back to 2 at 11.
Trade-offs. Do not finalize a node on first visit: the second visit is the point. Waiting uses the departure time; stored dist is the arrival. 64-bit times: time and change go to 10^9.
Show helper code
/** A real binary heap with a caller-supplied "less" comparator: O(log n) push/pop. */
class Heap<T> {
private a: T[] = [];
private less: (x: T, y: T) => boolean;
constructor(less: (x: T, y: T) => boolean) {
this.less = less;
}
get size(): number {
return this.a.length;
}
peek(): T {
return this.a[0]!;
}
push(x: T): void {
const a = this.a;
a.push(x);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.less(a[i]!, a[p]!)) break;
[a[i], a[p]] = [a[p]!, a[i]!];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0]!;
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
const l = 2 * i + 1;
const r = l + 1;
let s = i;
if (l < a.length && this.less(a[l]!, a[s]!)) s = l;
if (r < a.length && this.less(a[r]!, a[s]!)) s = r;
if (s === i) break;
[a[i], a[s]] = [a[s]!, a[i]!];
i = s;
}
}
return top;
}
}
/** A real binary heap with a caller-supplied "less" comparator: O(log n) push/pop. */
class Heap<T> {
private a: T[] = [];
private less: (x: T, y: T) => boolean;
constructor(less: (x: T, y: T) => boolean) {
this.less = less;
}
get size(): number {
return this.a.length;
}
peek(): T {
return this.a[0]!;
}
push(x: T): void {
const a = this.a;
a.push(x);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.less(a[i]!, a[p]!)) break;
[a[i], a[p]] = [a[p]!, a[i]!];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0]!;
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
const l = 2 * i + 1;
const r = l + 1;
let s = i;
if (l < a.length && this.less(a[l]!, a[s]!)) s = l;
if (r < a.length && this.less(a[r]!, a[s]!)) s = r;
if (s === i) break;
[a[i], a[s]] = [a[s]!, a[i]!];
i = s;
}
}
return top;
}
}
/**
* Real binary min-heap priority queue.
* Each node keeps first and second arrival; leave only on green.
*/
export function secondMinimum(
n: number,
edges: number[][],
time: number,
change: number,
): number {
const adj: number[][] = Array.from({ length: n + 1 }, () => []);
for (const [a, b] of edges) {
adj[a]!.push(b);
adj[b]!.push(a);
}
const dist1 = new Array<number>(n + 1).fill(Infinity);
const dist2 = new Array<number>(n + 1).fill(Infinity);
dist1[1] = 0;
const pq = new Heap<[number, number]>((a, b) => a[0]! < b[0]!);
pq.push([0, 1]);
while (pq.size) {
let [t, u] = pq.pop();
if (u === n && t > dist1[n]!) return t;
if (Math.floor(t / change) % 2 === 1) t += change - (t % change);
for (const v of adj[u]!) {
const nt = t + time;
if (nt < dist1[v]!) {
dist2[v] = dist1[v]!;
dist1[v] = nt;
pq.push([nt, v]);
} else if (nt > dist1[v]! && nt < dist2[v]!) {
dist2[v] = nt;
pq.push([nt, v]);
}
}
}
return dist2[n]!;
}
/**
* Real binary min-heap priority queue.
* Each node keeps first and second arrival; leave only on green.
*/
export function secondMinimum(
n: number,
edges: number[][],
time: number,
change: number,
): number {
const adj: number[][] = Array.from({ length: n + 1 }, () => []);
for (const [a, b] of edges) {
adj[a]!.push(b);
adj[b]!.push(a);
}
const dist1 = new Array<number>(n + 1).fill(Infinity);
const dist2 = new Array<number>(n + 1).fill(Infinity);
dist1[1] = 0;
const pq = new Heap<[number, number]>((a, b) => a[0]! < b[0]!);
pq.push([0, 1]);
while (pq.size) {
let [t, u] = pq.pop();
if (u === n && t > dist1[n]!) return t;
if (Math.floor(t / change) % 2 === 1) t += change - (t % change);
for (const v of adj[u]!) {
const nt = t + time;
if (nt < dist1[v]!) {
dist2[v] = dist1[v]!;
dist1[v] = nt;
pq.push([nt, v]);
} else if (nt > dist1[v]! && nt < dist2[v]!) {
dist2[v] = nt;
pq.push([nt, v]);
}
}
}
return dist2[n]!;
}
Template connection
Dijkstra + extra state, here a second distance per node instead of (node, k). Same “do not stop at first pop” idea as LC 787.
Reflection
- Keep
dist1anddist2. A candidate smaller than the first arrival shifts the first down. A time strictly between them becomes the second. An equal time is not a second arrival. - The answer is the first time you pop
nwitht > dist1[n]. That pop is the arrival. - The light is checked on departure: if
t // changeis odd, waitchange - t % changebefore leaving.n = 2is the direct edge.