Skip to content

Commit dad8df4

Browse files
committed
Add solution #743
1 parent 84388b1 commit dad8df4

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
733|[Flood Fill](./0733-flood-fill.js)|Easy|
274274
735|[Asteroid Collision](./0735-asteroid-collision.js)|Medium|
275275
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
276+
743|[Network Delay Time](./0743-network-delay-time.js)|Medium|
276277
744|[Find Smallest Letter Greater Than Target](./0744-find-smallest-letter-greater-than-target.js)|Easy|
277278
745|[Prefix and Suffix Search](./0745-prefix-and-suffix-search.js)|Hard|
278279
746|[Min Cost Climbing Stairs](./0746-min-cost-climbing-stairs.js)|Easy|

solutions/0743-network-delay-time.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 743. Network Delay Time
3+
* https://leetcode.com/problems/network-delay-time/
4+
* Difficulty: Medium
5+
*
6+
* You are given a network of n nodes, labeled from 1 to n. You are also given times, a
7+
* list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source
8+
* node, vi is the target node, and wi is the time it takes for a signal to travel from
9+
* source to target.
10+
*
11+
* We will send a signal from a given node k. Return the minimum time it takes for all
12+
* the n nodes to receive the signal. If it is impossible for all the n nodes to receive
13+
* the signal, return -1.
14+
*/
15+
16+
/**
17+
* @param {number[][]} times
18+
* @param {number} n
19+
* @param {number} k
20+
* @return {number}
21+
*/
22+
var networkDelayTime = function(times, n, k) {
23+
const time = new Array(n + 1).fill(Infinity);
24+
25+
time[k] = 0;
26+
27+
for (let i = 0; i < n; i++) {
28+
for (const [u, v, w] of times) {
29+
if (time[u] === Infinity) continue;
30+
if (time[v] > time[u] + w) {
31+
time[v] = time[u] + w;
32+
}
33+
}
34+
}
35+
36+
let result = 0;
37+
for (let i = 1; i <= n; i++) {
38+
result = Math.max(result, time[i]);
39+
}
40+
41+
return result === Infinity ? -1 : result;
42+
};

0 commit comments

Comments
 (0)