Skip to content

Commit b0b5724

Browse files
committedJul 13, 2023
💀 [743]
1 parent 5557b53 commit b0b5724

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed
 

‎743/my_solution.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @param {number[][]} times
3+
* @param {number} n
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var networkDelayTime = function (times, n, k) {
8+
let
9+
visited = Array.from({ length: n + 1 }, () => false),
10+
radList = Array.from({ length: n + 1 }, () => []),
11+
outdegree = Array.from({ length: n + 1 }, () => []),
12+
indegree = Array.from({ length: n + 1 }, () => []),
13+
timeMatrix = Array.from({ length: n + 1 }, () => Array.from({ length: n + 1 }, () => 0)),
14+
edges
15+
16+
console.log(timeMatrix);
17+
console.log(outdegree);
18+
19+
// [u, v, w]
20+
// construct outdegree
21+
for (let i = 0; i < times.length; i++) {
22+
outdegree[times[i][0]].push(times[i][1]);
23+
timeMatrix[times[i][0]][times[i][1]] = times[i][2]
24+
}
25+
26+
for (let i = 0; i < times.length; i++) {
27+
indegree[times[i][1]].push(times[i][0]);
28+
}
29+
30+
console.log(timeMatrix);
31+
console.log("outdegree");
32+
console.log(outdegree);
33+
console.log("indegree");
34+
console.log(indegree);
35+
// console.log("visited");
36+
// console.log(visited);
37+
38+
if (outdegree[k].length === 0) return -1;
39+
40+
// dfs
41+
let idx = 0, queue = [k], max = 0, toDeduct = 0
42+
while (queue.length > 0) {
43+
// console.log("> visited");
44+
// console.log(visited);
45+
46+
let curr = queue.shift();
47+
// visited[curr] = true;
48+
console.log("curr")
49+
console.log(curr)
50+
// let curr = outdegree.shift();
51+
// if (curr.length > 0) {
52+
let tmpMax = 0
53+
let child = outdegree[curr];
54+
console.log("child")
55+
console.log(child)
56+
while (child.length > 0) {
57+
let childIdx = child.shift();
58+
// if (visited[childIdx]) break;
59+
// visited[childIdx] = true;
60+
61+
console.log("> childIdx")
62+
console.log(childIdx)
63+
radList[curr].push(timeMatrix[curr][childIdx])
64+
tmpMax = Math.max(tmpMax, timeMatrix[curr][childIdx])
65+
queue.push(childIdx)
66+
console.log("> queue")
67+
console.log(queue)
68+
}
69+
70+
max += tmpMax;
71+
// }
72+
// else do nth
73+
}
74+
75+
// console.log("visited");
76+
// console.log(visited);
77+
console.log("max")
78+
console.log(max)
79+
console.log("radList")
80+
console.log(radList)
81+
82+
// while (indegree.length > 0) {
83+
// let curr = indegree.shift();
84+
//
85+
// if (curr.length > 1) {
86+
// let tmpMax = 0
87+
// while (curr.length > 0) {
88+
// let child = curr.shift();
89+
// tmpMax = Math.max(tmpMax, timeMatrix[idx][child])
90+
// }
91+
// toDeduct += tmpMax;
92+
// }
93+
//
94+
// idx++;
95+
// // else do nth
96+
// }
97+
98+
return max - toDeduct;
99+
};
100+
101+
let x =
102+
networkDelayTime([[1, 2, 1], [2, 3, 2], [1, 3, 4]], 3, 1) // 3
103+
// networkDelayTime([[2, 1, 1], [2, 3, 1], [3, 4, 1]], 4, 2) // 2
104+
// networkDelayTime([[1, 2, 1], [2, 1, 3]], 2, 2) // 3
105+
// networkDelayTime([[1,2, 1]], 2, 2) // - 1
106+
107+
console.log("res")
108+
console.log(x)

‎743/solution.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @param {number[][]} times
3+
* @param {number} n
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var networkDelayTime = function (times, n, k) {
8+
let
9+
visited = Array.from({ length: n + 1 }, () => false),
10+
outdegree = Array.from({ length: n + 1 }, () => []),
11+
indegree = Array.from({ length: n + 1 }, () => []),
12+
timeMatrix = Array.from({ length: n + 1 }, () => Array.from({ length: n + 1 }, () => 0)),
13+
edges
14+
15+
console.log(timeMatrix);
16+
console.log(outdegree);
17+
18+
// [u, v, w]
19+
// construct outdegree
20+
for (let i = 0; i < times.length; i++) {
21+
outdegree[times[i][0]].push(times[i][1]);
22+
timeMatrix[times[i][0]][times[i][1]] = times[i][2]
23+
}
24+
25+
for (let i = 0; i < times.length; i++) {
26+
indegree[times[i][1]].push(times[i][0]);
27+
}
28+
29+
if (outdegree[k].length === 0) return -1;
30+
31+
// dfs
32+
let idx = 0, queue = [k], max = 0, toDeduct = 0
33+
while (queue.length > 0) {
34+
35+
let curr = queue.shift();
36+
visited[curr] = true;
37+
38+
let tmpMax = 0
39+
let child = outdegree[curr];
40+
41+
while (child.length > 0) {
42+
let childIdx = child.shift();
43+
if (visited[childIdx]) break;
44+
visited[childIdx] = true;
45+
46+
tmpMax = Math.max(tmpMax, timeMatrix[curr][childIdx])
47+
queue.push(childIdx)
48+
}
49+
50+
max += tmpMax;
51+
}
52+
53+
54+
return max - toDeduct;
55+
};

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
- [714. Best Time to Buy and Sell Stock with Transaction Fee](./714/)
9595
- [724. Find Pivot Index](./724/)
9696
- [735. Asteroid Collision](./735/)
97+
- [743. Network Delay Time](./743/)
9798
- [771. Jewels and Stones](./771/)
9899
- [832. Flipping an Image](./832/)
99100
- [876. Middle of the Linked List](./876/)
@@ -174,7 +175,7 @@ Batch create:
174175
NOTE: JS IS HERE
175176
-->
176177
```ssh
177-
chapter=735 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
178+
chapter=743 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
178179
```
179180
> then you can use `x` for quick debug.
180181

0 commit comments

Comments
 (0)
Please sign in to comment.