-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem_270.js
More file actions
53 lines (45 loc) Β· 1.19 KB
/
problem_270.js
File metadata and controls
53 lines (45 loc) Β· 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Helper function to calculate distances
* @param {number} target target node
* @param {Object} edgeList list of edges
*/
function calculateDist(target, edgeList) {
if (target === 0) return 0;
const targetDistances = edgeList[target];
const distances = [];
for (let i = 0; i < targetDistances.length; i++) {
const [currTarget, currDist] = targetDistances[i];
const dist = currDist + calculateDist(currTarget, edgeList);
distances.push(dist);
}
return Math.min(...distances);
}
/**
* Determines how long it will take for every node to receive message
* @param {number[][]} edges list of edges (a, b, t)
* @param {number} count
*/
function findShortestPath(edges, count) {
const edgeList = {};
edges.forEach(edge => {
const [start, end, dist] = edge;
if (!edgeList[end]) edgeList[end] = [];
edgeList[end].push([start, dist]);
});
const distances = [];
for (let i = 1; i <= count; i++) {
const dist = calculateDist(i, edgeList);
distances.push(dist);
}
return Math.max(...distances);
}
const edges = [
[0, 1, 5],
[0, 2, 3],
[0, 5, 4],
[1, 3, 8],
[2, 3, 1],
[3, 5, 10],
[3, 4, 5]
];
console.log(findShortestPath(edges, 5)); // 9