1
var minReorder = function (n, connections) {2
// from: (<from city>, [<to cities>])3
// to: (<to city>, [<from cities>])4
const from = new Map(),7
// Function to insert in values in map8
const insert = (map, key, value) => {10
const arr = map.get(key);14
map.set(key, [value]);18
// Set all values in both maps19
for (const [a, b] of connections) {24
// Queue: cities to visit29
while (queue.length) {30
const cur = queue.shift(); // First element in queue32
// Check values in first map34
for (const x of from.get(cur)) {35
// If visited, do nothing else add to queue36
if (visited.has(x)) continue;38
count++; // Change direction of this path43
// If visited, do nothing else add to queue44
for (const x of to.get(cur)) {45
if (visited.has(x)) continue;50
visited.add(cur); // Mark city as visited