-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathReorder Routes to Make All Paths Lead to the City Zero.js
54 lines (45 loc) · 1.42 KB
/
Reorder Routes to Make All Paths Lead to the City Zero.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
// Runtime: 267 ms (Top 86.1%) | Memory: 107.22 MB (Top 89.0%)
var minReorder = function(n, connections) {
// from: (<from city>, [<to cities>])
// to: (<to city>, [<from cities>])
const from = new Map(), to = new Map();
// Function to insert in values in map
const insert = (map, key, value) => {
if(map.has(key)){
const arr = map.get(key);
arr.push(value);
map.set(key, arr);
} else {
map.set(key, [value]);
}
}
// Set all values in both maps
for(const [a,b] of connections){
insert(from, a, b);
insert(to, b, a);
}
// Queue: cities to visit
const queue = [0], visited = new Set();
let count = 0;
while(queue.length) {
const cur = queue.shift(); // First element in queue
// Check values in first map
if(from.has(cur)){
for(const x of from.get(cur)){
// If visited, do nothing else add to queue
if(visited.has(x)) continue;
queue.push(x);
count++; // Change direction of this path
}
}
if(to.has(cur)){
// If visited, do nothing else add to queue
for(const x of to.get(cur)){
if(visited.has(x)) continue;
queue.push(x);
}
}
visited.add(cur); // Mark city as visited
}
return count
};