Skip to content

Commit ede258d

Browse files
authored
Update 743-network-delay-time.js
1 parent 1013531 commit ede258d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

743-network-delay-time.js

+30
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,33 @@ const networkDelayTime = function (times, N, K) {
1717
}
1818
return mins.includes(Infinity) ? -1 : Math.max(...mins)
1919
}
20+
21+
// another
22+
23+
/**
24+
* @param {number[][]} times
25+
* @param {number} N
26+
* @param {number} K
27+
* @return {number}
28+
*/
29+
const networkDelayTime = function(times, N, K) {
30+
const distances = new Array(N).fill(Infinity);
31+
distances[K - 1] = 0;
32+
33+
for(let i = 0 ; i < N -1 ; i++){
34+
let counter = 0;
35+
for(let j = 0 ; j < times.length ; j++){
36+
const source = times[j][0];
37+
const target = times[j][1];
38+
const weight = times[j][2];
39+
if(distances[source - 1] + weight < distances[target - 1]){
40+
distances[target - 1] = distances[source - 1] + weight;
41+
counter++
42+
}
43+
}
44+
if(counter === 0) break
45+
}
46+
47+
const res = Math.max(...distances);
48+
return res === Infinity ? -1 : res;
49+
};

0 commit comments

Comments
 (0)