-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
108 lines (94 loc) · 2.7 KB
/
my_solution.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @param {number[][]} times
* @param {number} n
* @param {number} k
* @return {number}
*/
var networkDelayTime = function (times, n, k) {
let
visited = Array.from({ length: n + 1 }, () => false),
radList = Array.from({ length: n + 1 }, () => []),
outdegree = Array.from({ length: n + 1 }, () => []),
indegree = Array.from({ length: n + 1 }, () => []),
timeMatrix = Array.from({ length: n + 1 }, () => Array.from({ length: n + 1 }, () => 0)),
edges
console.log(timeMatrix);
console.log(outdegree);
// [u, v, w]
// construct outdegree
for (let i = 0; i < times.length; i++) {
outdegree[times[i][0]].push(times[i][1]);
timeMatrix[times[i][0]][times[i][1]] = times[i][2]
}
for (let i = 0; i < times.length; i++) {
indegree[times[i][1]].push(times[i][0]);
}
console.log(timeMatrix);
console.log("outdegree");
console.log(outdegree);
console.log("indegree");
console.log(indegree);
// console.log("visited");
// console.log(visited);
if (outdegree[k].length === 0) return -1;
// dfs
let idx = 0, queue = [k], max = 0, toDeduct = 0
while (queue.length > 0) {
// console.log("> visited");
// console.log(visited);
let curr = queue.shift();
// visited[curr] = true;
console.log("curr")
console.log(curr)
// let curr = outdegree.shift();
// if (curr.length > 0) {
let tmpMax = 0
let child = outdegree[curr];
console.log("child")
console.log(child)
while (child.length > 0) {
let childIdx = child.shift();
// if (visited[childIdx]) break;
// visited[childIdx] = true;
console.log("> childIdx")
console.log(childIdx)
radList[curr].push(timeMatrix[curr][childIdx])
tmpMax = Math.max(tmpMax, timeMatrix[curr][childIdx])
queue.push(childIdx)
console.log("> queue")
console.log(queue)
}
max += tmpMax;
// }
// else do nth
}
// console.log("visited");
// console.log(visited);
console.log("max")
console.log(max)
console.log("radList")
console.log(radList)
// while (indegree.length > 0) {
// let curr = indegree.shift();
//
// if (curr.length > 1) {
// let tmpMax = 0
// while (curr.length > 0) {
// let child = curr.shift();
// tmpMax = Math.max(tmpMax, timeMatrix[idx][child])
// }
// toDeduct += tmpMax;
// }
//
// idx++;
// // else do nth
// }
return max - toDeduct;
};
let x =
networkDelayTime([[1, 2, 1], [2, 3, 2], [1, 3, 4]], 3, 1) // 3
// networkDelayTime([[2, 1, 1], [2, 3, 1], [3, 4, 1]], 4, 2) // 2
// networkDelayTime([[1, 2, 1], [2, 1, 3]], 2, 2) // 3
// networkDelayTime([[1,2, 1]], 2, 2) // - 1
console.log("res")
console.log(x)