743. Network Delay Time
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Advanced Graphs
- Time
- —
- Space
- —
Standard Solution (C++)
SummaryGiven a directed weighted graph of `n` nodes and a list of directed edges, find the minimum time for a signal sent from node `k` to reach all other nodes, or return -1 if any node is unreachable.
Approach
- Build an adjacency list from the edge list, then run Dijkstra's algorithm from source node
kusing a min-heap keyed on tentative distance. - Each time a node is relaxed, push the updated
{distance, node}pair onto the heap; stale entries withd > dist[u]are skipped immediately. - After the heap empties, scan the distance array: if any node remains at infinity the graph is disconnected and we return -1; otherwise the answer is the maximum shortest-path distance across all nodes (the bottleneck that determines when the last node receives the signal).
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
// Build adjacency list: adj[u] = list of {v, weight}
vector<vector<pair<int,int>>> adj(n + 1);
for (const auto& e : times)
adj[e[0]].emplace_back(e[1], e[2]);
// Dijkstra from source k
vector<int> dist(n + 1, INT_MAX);
dist[k] = 0;
// Min-heap storing {tentative_distance, node}
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
pq.push({0, k});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
// Skip stale heap entries
if (d > dist[u]) continue;
for (auto [v, w] : adj[u]) {
int nd = dist[u] + w;
if (nd < dist[v]) {
dist[v] = nd;
pq.push({nd, v});
}
}
}
// The signal arrival time is the maximum shortest-path distance
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (dist[i] == INT_MAX) return -1; // unreachable node
ans = max(ans, dist[i]);
}
return ans;
}
};
Time O(E log V) Space O(V + E)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
#include <vector>
#include <queue>
// #include <climits>
using namespace std;
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
// Dijkstra: single source
// keep adding the min time edge to our graph
// to achieve this, we need to maintain a pq
// time: O(ElogV)
// space: O(V+E)
// init adj list
vector<vector<pair<int, int>>> adj(n+1);
for(const auto& t: times){
adj[t[0]].push_back({t[1], t[2]}); // key is the source node
}
// init dist
vector<int> dist(n+1, INT_MAX);
dist[k]=0;
// init pq, it stores {w, u}
std::priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // min_heap
pq.push({0, k});
// do Dijkstra's Algo
while(!pq.empty()){
const auto [d, u]=pq.top();
pq.pop();
if(d>dist[u]){
continue;
}
for(const auto& edge: adj[u]){
int v=edge.first;
int w=edge.second;
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
pq.push({dist[v], v});
}
}
}
int maxDist=INT_MIN;
for(int i=1; i<=n; ++i){
if(dist[i]==INT_MAX){
return -1;
}
maxDist=std::max(maxDist, dist[i]);
}
return maxDist;
}
};
// Time: O(ElogV) or O(ElogE) since E can be at most V^2, O(ElogV^2) = O(2ElogV) = O(ElogV)
// Space: O(V+E) for the adjacency list and distance array My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.