LeetCode Entry
2685. Count the Number of Complete Components
Fully connected components
2685. Count the Number of Complete Components medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1417
Problem TLDR
Fully connected components
Intuition
- Union-Find to find connected components
- Count incoming edges for each node
- Fully connected group size is the number of edges for each node +1
Approach
- 50 elements can fit into long variable
- adjacency matrix would look the same for each group
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun countCompleteComponents(n: Int, e: Array<IntArray>) = e
.fold(LongArray(n){1L shl it}){ m, (a, b) -> m[a] += 1L shl b; m[b] += 1L shl a; m }
.groupBy { it }.count { (k, v) -> v.size == k.countOneBits() }
6
Comments