#2249
Medium Algorithms Count lattice points inside a circle
Array Hash Table Math Geometry Enumeration
56.5% acceptance
Feb 25, 2026
253
223
Given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and radius ri of the ith circle drawn on a grid, return the number of lattice points that are present inside at least one circle.
Note:
A lattice point is a point with integer coordinates.
Points that lie on the circumference of a circle are also considered to be inside it.
Solution
Rust
Time O(n³)
Space O(n)
impl Solution {
pub fn count_lattice_points(circles: Vec<Vec<i32>>) -> i32 {
use std::collections::HashSet;
let mut points: HashSet<(i32, i32)> = HashSet::new();
for c in &circles {
let (cx, cy, r) = (c[0], c[1], c[2]);
for x in (cx - r)..=(cx + r) {
let dx = x - cx;
let max_dy = ((r * r - dx * dx) as f64).sqrt() as i32;
for y in (cy - max_dy)..=(cy + max_dy) {
points.insert((x, y));
}
}
}
points.len() as i32
}
}