ds-algo-study
Version:
Just experimenting with publishing a package
27 lines (19 loc) • 462 B
JavaScript
function numRegions(graph) {
let visited = new Set();
let regions = 0;
for (let node in graph) {
if (isNewRegion(node, graph, visited)) regions++;
}
return regions;
}
function isNewRegion(node, graph, visited) {
if (visited.has(node)) return false;
visited.add(node);
graph[node].forEach((neighbor) => {
isNewRegion(neighbor, graph, visited);
});
return true;
}
module.exports = {
numRegions
};