Radius Clusters

Asked at Google

Reported by the problem's creator, not verified

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.

Examples

  1. Example 1

    Input: points = [[0,0],[1,0],[5,5],[6,5],[20,20]], radius = 1

    Output: 3

    Explanation: [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.

  2. Example 2

    Input: points = [[0,0],[3,0],[6,0],[9,0]], radius = 3

    Output: 1

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.