Sensor Network II
Field sensors numbered 0 to n − 1 are linked by undirected edges (u, v). Return the number of disconnected components — groups of sensors that can reach each other through links. A sensor with no links is a component of its own.
n = 5, edges = [[0, 1], [1, 2], [2, 0]] -> 3 ({0, 1, 2}, {3}, {4})
n = 4, edges = [[0, 1], [2, 3]] -> 2Hints
Hint 1
Start with n components, one per sensor. Every edge that joins two different components reduces the count by one.
Hint 2
Union-Find tells you whether an edge joins two different components; a BFS or DFS from each unvisited sensor works too.
Topics
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.