Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.
[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.
Notes:
1 and n.Example 1:
Input: n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 Output: 13 Explanation: The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -> 4: 3 minutes, time elapsed=3 - 4 -> 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -> 3: 3 minutes, time elapsed=3 - 3 -> 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -> 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes.
Example 2:
Input: n = 2, edges = [[1,2]], time = 3, change = 2 Output: 11 Explanation: The minimum time path is 1 -> 2 with time = 3 minutes. The second minimum time path is 1 -> 2 -> 1 -> 2 with time = 11 minutes.
Constraints:
2 <= n <= 104n - 1 <= edges.length <= min(2 * 104, n * (n - 1) / 2)edges[i].length == 21 <= ui, vi <= nui != vi1 <= time, change <= 103Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
5 [[1,2],[1,3],[1,4],[3,4],[4,5]] 3 5
2 [[1,2]] 3 2
network-delay-time)find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance)number-of-ways-to-arrive-at-destination)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
class Solution {
public int secondMinimum(int n, int[][] edges, int time, int change) {
List<Integer>[] g = new List[n + 1];
Arrays.setAll(g, k -> new ArrayList<>());
for (int[] e : edges) {
int u = e[0], v = e[1];
g[u].add(v);
g[v].add(u);
}
Deque<int[]> q = new LinkedList<>();
q.offerLast(new int[] {1, 0});
int[][] dist = new int[n + 1][2];
for (int i = 0; i < n + 1; ++i) {
Arrays.fill(dist[i], Integer.MAX_VALUE);
}
dist[1][1] = 0;
while (!q.isEmpty()) {
int[] e = q.pollFirst();
int u = e[0], d = e[1];
for (int v : g[u]) {
if (d + 1 < dist[v][0]) {
dist[v][0] = d + 1;
q.offerLast(new int[] {v, d + 1});
} else if (dist[v][0] < d + 1 && d + 1 < dist[v][1]) {
dist[v][1] = d + 1;
if (v == n) {
break;
}
q.offerLast(new int[] {v, d + 1});
}
}
}
int ans = 0;
for (int i = 0; i < dist[n][1]; ++i) {
ans += time;
if (i < dist[n][1] - 1 && (ans / change) % 2 == 1) {
ans = (ans + change) / change * change;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
// class Solution {
// public int secondMinimum(int n, int[][] edges, int time, int change) {
// List<Integer>[] g = new List[n + 1];
// Arrays.setAll(g, k -> new ArrayList<>());
// for (int[] e : edges) {
// int u = e[0], v = e[1];
// g[u].add(v);
// g[v].add(u);
// }
// Deque<int[]> q = new LinkedList<>();
// q.offerLast(new int[] {1, 0});
// int[][] dist = new int[n + 1][2];
// for (int i = 0; i < n + 1; ++i) {
// Arrays.fill(dist[i], Integer.MAX_VALUE);
// }
// dist[1][1] = 0;
// while (!q.isEmpty()) {
// int[] e = q.pollFirst();
// int u = e[0], d = e[1];
// for (int v : g[u]) {
// if (d + 1 < dist[v][0]) {
// dist[v][0] = d + 1;
// q.offerLast(new int[] {v, d + 1});
// } else if (dist[v][0] < d + 1 && d + 1 < dist[v][1]) {
// dist[v][1] = d + 1;
// if (v == n) {
// break;
// }
// q.offerLast(new int[] {v, d + 1});
// }
// }
// }
// int ans = 0;
// for (int i = 0; i < dist[n][1]; ++i) {
// ans += time;
// if (i < dist[n][1] - 1 && (ans / change) % 2 == 1) {
// ans = (ans + change) / change * change;
// }
// }
// return ans;
// }
// }
# Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
class Solution:
def secondMinimum(
self, n: int, edges: List[List[int]], time: int, change: int
) -> int:
g = defaultdict(set)
for u, v in edges:
g[u].add(v)
g[v].add(u)
q = deque([(1, 0)])
dist = [[inf] * 2 for _ in range(n + 1)]
dist[1][1] = 0
while q:
u, d = q.popleft()
for v in g[u]:
if d + 1 < dist[v][0]:
dist[v][0] = d + 1
q.append((v, d + 1))
elif dist[v][0] < d + 1 < dist[v][1]:
dist[v][1] = d + 1
if v == n:
break
q.append((v, d + 1))
ans = 0
for i in range(dist[n][1]):
ans += time
if i < dist[n][1] - 1 and (ans // change) % 2 == 1:
ans = (ans + change) // change * change
return ans
// Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
/**
* [2045] Second Minimum Time to Reach Destination
*
* 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.
* Notes:
*
* 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.
*
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" />        <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" />
* Input: n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5
* Output: 13
* Explanation:
* The figure on the left shows the given graph.
* The blue path in the figure on the right is the minimum time path.
* The time taken is:
* - Start at 1, time elapsed=0
* - 1 -> 4: 3 minutes, time elapsed=3
* - 4 -> 5: 3 minutes, time elapsed=6
* Hence the minimum time needed is 6 minutes.
* The red path shows the path to get the second minimum time.
* - Start at 1, time elapsed=0
* - 1 -> 3: 3 minutes, time elapsed=3
* - 3 -> 4: 3 minutes, time elapsed=6
* - Wait at 4 for 4 minutes, time elapsed=10
* - 4 -> 5: 3 minutes, time elapsed=13
* Hence the second minimum time is 13 minutes.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" />
* Input: n = 2, edges = [[1,2]], time = 3, change = 2
* Output: 11
* Explanation:
* The minimum time path is 1 -> 2 with time = 3 minutes.
* The second minimum time path is 1 -> 2 -> 1 -> 2 with time = 11 minutes.
*
* Constraints:
*
* 2 <= n <= 10^4
* n - 1 <= edges.length <= min(2 * 10^4, n * (n - 1) / 2)
* edges[i].length == 2
* 1 <= ui, vi <= n
* ui != vi
* There are no duplicate edges.
* Each vertex can be reached directly or indirectly from every other vertex.
* 1 <= time, change <= 10^3
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/second-minimum-time-to-reach-destination/
// discuss: https://leetcode.com/problems/second-minimum-time-to-reach-destination/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn second_minimum(n: i32, edges: Vec<Vec<i32>>, time: i32, change: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2045_example_1() {
let n = 5;
let edges = vec![vec![1, 2], vec![1, 3], vec![1, 4], vec![3, 4], vec![4, 5]];
let time = 3;
let change = 5;
let result = 13;
assert_eq!(Solution::second_minimum(n, edges, time, change), result);
}
#[test]
#[ignore]
fn test_2045_example_2() {
let n = 2;
let edges = vec![vec![1, 2]];
let time = 3;
let change = 2;
let result = 11;
assert_eq!(Solution::second_minimum(n, edges, time, change), result);
}
}
// Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2045: Second Minimum Time to Reach Destination
// class Solution {
// public int secondMinimum(int n, int[][] edges, int time, int change) {
// List<Integer>[] g = new List[n + 1];
// Arrays.setAll(g, k -> new ArrayList<>());
// for (int[] e : edges) {
// int u = e[0], v = e[1];
// g[u].add(v);
// g[v].add(u);
// }
// Deque<int[]> q = new LinkedList<>();
// q.offerLast(new int[] {1, 0});
// int[][] dist = new int[n + 1][2];
// for (int i = 0; i < n + 1; ++i) {
// Arrays.fill(dist[i], Integer.MAX_VALUE);
// }
// dist[1][1] = 0;
// while (!q.isEmpty()) {
// int[] e = q.pollFirst();
// int u = e[0], d = e[1];
// for (int v : g[u]) {
// if (d + 1 < dist[v][0]) {
// dist[v][0] = d + 1;
// q.offerLast(new int[] {v, d + 1});
// } else if (dist[v][0] < d + 1 && d + 1 < dist[v][1]) {
// dist[v][1] = d + 1;
// if (v == n) {
// break;
// }
// q.offerLast(new int[] {v, d + 1});
// }
// }
// }
// int ans = 0;
// for (int i = 0; i < dist[n][1]; ++i) {
// ans += time;
// if (i < dist[n][1] - 1 && (ans / change) % 2 == 1) {
// ans = (ans + change) / change * change;
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.