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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from source to target.
We will send a signal from a given node k. Return the minimum time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1.
Example 1:
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2 Output: 2
Example 2:
Input: times = [[1,2,1]], n = 2, k = 1 Output: 1
Example 3:
Input: times = [[1,2,1]], n = 2, k = 2 Output: -1
Constraints:
1 <= k <= n <= 1001 <= times.length <= 6000times[i].length == 31 <= ui, vi <= nui != vi0 <= wi <= 100(ui, vi) are unique. (i.e., no multiple edges.)Problem summary: You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from source to target. We will send a signal from a given node k. Return the minimum time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[[2,1,1],[2,3,1],[3,4,1]] 4 2
[[1,2,1]] 2 1
[[1,2,1]] 2 2
the-time-when-the-network-becomes-idle)second-minimum-time-to-reach-destination)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #743: Network Delay Time
class Solution {
public int networkDelayTime(int[][] times, int n, int k) {
int[][] g = new int[n][n];
int[] dist = new int[n];
final int inf = 1 << 29;
Arrays.fill(dist, inf);
for (var e : g) {
Arrays.fill(e, inf);
}
for (var e : times) {
g[e[0] - 1][e[1] - 1] = e[2];
}
dist[k - 1] = 0;
boolean[] vis = new boolean[n];
for (int i = 0; i < n; ++i) {
int t = -1;
for (int j = 0; j < n; ++j) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
vis[t] = true;
for (int j = 0; j < n; ++j) {
dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
}
}
int ans = 0;
for (int x : dist) {
ans = Math.max(ans, x);
}
return ans == inf ? -1 : ans;
}
}
// Accepted solution for LeetCode #743: Network Delay Time
func networkDelayTime(times [][]int, n int, k int) int {
const inf = 1 << 29
g := make([][]int, n)
for i := range g {
g[i] = make([]int, n)
for j := range g[i] {
g[i][j] = inf
}
}
for _, e := range times {
g[e[0]-1][e[1]-1] = e[2]
}
dist := make([]int, n)
for i := range dist {
dist[i] = inf
}
dist[k-1] = 0
vis := make([]bool, n)
for i := 0; i < n; i++ {
t := -1
for j := 0; j < n; j++ {
if !vis[j] && (t == -1 || dist[t] > dist[j]) {
t = j
}
}
vis[t] = true
for j := 0; j < n; j++ {
dist[j] = min(dist[j], dist[t]+g[t][j])
}
}
if ans := slices.Max(dist); ans != inf {
return ans
}
return -1
}
# Accepted solution for LeetCode #743: Network Delay Time
class Solution:
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
g = [[inf] * n for _ in range(n)]
for u, v, w in times:
g[u - 1][v - 1] = w
dist = [inf] * n
dist[k - 1] = 0
vis = [False] * n
for _ in range(n):
t = -1
for j in range(n):
if not vis[j] and (t == -1 or dist[t] > dist[j]):
t = j
vis[t] = True
for j in range(n):
dist[j] = min(dist[j], dist[t] + g[t][j])
ans = max(dist)
return -1 if ans == inf else ans
// Accepted solution for LeetCode #743: Network Delay Time
struct Solution;
use std::collections::VecDeque;
impl Solution {
fn network_delay_time(times: Vec<Vec<i32>>, n: i32, k: i32) -> i32 {
let n = n as usize;
let k = k as usize - 1;
let mut graph: Vec<Vec<(usize, i32)>> = vec![vec![]; n];
for time in times {
let u = time[0] as usize - 1;
let v = time[1] as usize - 1;
let t = time[2];
graph[u].push((v, t));
}
let mut visited = vec![std::i32::MAX; n];
let mut queue = VecDeque::new();
visited[k] = 0;
queue.push_back(k);
while let Some(u) = queue.pop_front() {
for &(v, t) in &graph[u] {
if t + visited[u] < visited[v] {
visited[v] = t + visited[u];
queue.push_back(v);
}
}
}
let max = visited.into_iter().max().unwrap();
if max == std::i32::MAX {
-1
} else {
max
}
}
}
#[test]
fn test() {
let times = vec_vec_i32![[2, 1, 1], [2, 3, 1], [3, 4, 1]];
let n = 4;
let k = 2;
let res = 2;
assert_eq!(Solution::network_delay_time(times, n, k), res);
}
// Accepted solution for LeetCode #743: Network Delay Time
function networkDelayTime(times: number[][], n: number, k: number): number {
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(Infinity));
for (const [u, v, w] of times) {
g[u - 1][v - 1] = w;
}
const dist: number[] = Array(n).fill(Infinity);
dist[k - 1] = 0;
const vis: boolean[] = Array(n).fill(false);
for (let i = 0; i < n; ++i) {
let t = -1;
for (let j = 0; j < n; ++j) {
if (!vis[j] && (t === -1 || dist[j] < dist[t])) {
t = j;
}
}
vis[t] = true;
for (let j = 0; j < n; ++j) {
dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
}
}
const ans = Math.max(...dist);
return ans === Infinity ? -1 : 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.