Class Solution
-
- All Implemented Interfaces:
public final class Solution3378 - Count Connected Components in LCM Graph.
Hard
You are given an array of integers
numsof sizenand a positive integerthreshold.There is a graph consisting of
nnodes with the <code>i<sup>th</sup></code> node having a value ofnums[i]. Two nodesiandjin the graph are connected via an undirected edge iflcm(nums[i], nums[j]) <= threshold.Return the number of connected components in this graph.
A connected component is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.
The term
lcm(a, b)denotes the least common multiple ofaandb.Example 1:
Input: nums = 2,4,8,3,9, threshold = 5
Output: 4
Explanation:
The four connected components are
(2, 4),(3),(8),(9).Example 2:
Input: nums = 2,4,8,3,9,12, threshold = 10
Output: 2
Explanation:
The two connected components are
(2, 3, 4, 8, 9), and(12).Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
All elements of
numsare unique.<code>1 <= threshold <= 2 * 10<sup>5</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegercountComponents(IntArray nums, Integer threshold)-
-
Method Detail
-
countComponents
final Integer countComponents(IntArray nums, Integer threshold)
-
-
-
-