Radius Clusters
GoogleMedium
You are given points, a list of [x, y] integer coordinates, and an integer radius.
python def count_clusters(points: list[list[int]], radius: int) -> int: raise NotImplementedError
Two points are linked when the straight-line distance between them is at most radius. Linking is transitive: a cluster is a group of points where each point is linked to another point of the group, directly or through a chain of linked points, and no point is linked to a point outside it.
Return the number of clusters. A point with no links is a cluster of one. Points with the same coordinates are separate points at distance 0 from each other, so they are always in the same cluster.
Part 1: Count clusters
Given a list of [x, y] integer coordinates points and an integer radius, return the number of clusters.
Two points are linked when the straight-line distance between them is at most radius. Linking is transitive: a cluster is a group of points where each point reaches every other point of the group through a chain of linked points, and no point of the group is linked to a point outside it.
A point with no links is a cluster of one. Points with the same coordinates are separate points at distance 0 from each other, so they always end up in the same cluster. An empty points list has 0 clusters.
- Example 1
- points
[[0,0],[1,0],[5,5],[6,5],[20,20]]- radius
1- Output
3
Why:
[0, 0]and[1, 0]are 1 apart, and[5, 5]and[6, 5]are 1 apart.[20, 20]is far from everything, so it forms a cluster of its own: 3 clusters. - Example 2
- points
[[0,0],[3,0],[6,0],[9,0]]- radius
3- Output
1
Why: Each point is 3 from the next, so a chain joins all four even though
[0, 0]and[9, 0]are 9 apart: 1 cluster.
Constraints
0 <= len(points) <= 2000-10^6 <= points[i][0] <= 10^6-10^6 <= points[i][1] <= 10^60 <= radius <= 10^7