Dashboard
LC #323
Number of Connected Components in Undirected Graph
Pattern · ?LinkedInGoogleAmazon
Problem
Given n nodes and a list of edges forming an undirected graph, return the number of connected components.
Constraints
- 1 ≤ n ≤ 2000
- 1 ≤ edges.length ≤ 5000
- No duplicate edges
- No self-loops
Example
- Input
- n = 5, edges = [[0,1],[1,2],[3,4]]
- Output
- 2
- Why
- Component 1: {0,1,2}. Component 2: {3,4}. Total = 2 components.
Spot the pattern
Which pattern cracks it?
Hints
One at a time