Count Lattice Points Inside a Circle - Problem

You're given a collection of circles on a 2D coordinate grid, and you need to count how many lattice points (points with integer coordinates) fall inside at least one of these circles.

Each circle is defined by its center coordinates (x, y) and radius r. A point is considered inside a circle if the distance from the point to the circle's center is less than or equal to the radius - this means points on the circumference count too!

Goal: Return the total count of unique lattice points that are covered by one or more circles.

Example: If you have a circle centered at (0, 0) with radius 2, it would contain lattice points like (0,0), (1,0), (0,1), (2,0), etc.

Input & Output

example_1.py โ€” Single Circle
$ Input: circles = [[2,2,1]]
โ€บ Output: 5
๐Ÿ’ก Note: The circle centered at (2,2) with radius 1 contains lattice points: (1,2), (2,1), (2,2), (2,3), (3,2). Total = 5 points.
example_2.py โ€” Multiple Overlapping Circles
$ Input: circles = [[2,2,2],[3,4,1]]
โ€บ Output: 16
๐Ÿ’ก Note: First circle contains 13 points, second circle contains 5 points, but 2 points are shared between both circles. Total unique points = 13 + 5 - 2 = 16.
example_3.py โ€” Non-overlapping Circles
$ Input: circles = [[1,1,1],[10,10,1]]
โ€บ Output: 10
๐Ÿ’ก Note: Each circle contains 5 lattice points and they don't overlap since they're far apart. Total = 5 + 5 = 10 points.

Visualization

Tap to expand
Sprinkler 1 (Blue)Sprinkler 2 (Red)Lattice Points in Sprinkler CoverageBlack dots: lattice points covered by sprinklers
Understanding the Visualization
1
Place Sprinklers
Each circle represents a sprinkler with its coverage area
2
Find Bounding Boxes
For efficiency, we only check points within each sprinkler's square bounding area
3
Count Coverage
Use a set to count unique grid points that get watered by at least one sprinkler
Key Takeaway
๐ŸŽฏ Key Insight: Bounding box optimization reduces the search space from the entire coordinate plane to just the areas around each circle, making the solution much more efficient!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(C ร— Rยฒ)

Where C is number of circles and R is the maximum radius. We check at most (2R+1)ยฒ points per circle.

n
2n
โœ“ Linear Growth
Space Complexity
O(P)

Where P is the number of unique lattice points inside circles (for the hash set)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค circles.length โ‰ค 200
  • circles[i].length == 3
  • 1 โ‰ค xi, yi โ‰ค 100
  • 1 โ‰ค ri โ‰ค min(xi, yi)
  • Important: Points on the circumference are considered inside the circle
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
24.7K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen