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 _thenumber of lattice points _that are present insideat 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.

Input: circles =[[2,2,1]]Output: 5Explanation:
The figure above shows the given circle.The lattice points present inside the circle are(1,2),(2,1),(2,2),(2,3), and (3,2) and are shown in green.Other points such as(1,1) and (1,3), which are shown in red, are not considered inside the circle.Hence, the number of lattice points present inside at least one circle is5.

Input: circles =[[2,2,2],[3,4,1]]Output: 16Explanation:
The figure above shows the given circles.There are exactly 16 lattice points which are present inside at least one circle.Some of them are(0,2),(2,0),(2,4),(3,2), and (4,4).
For each circle, enumerate all integer points within its bounding box and check if they are inside or on the circle. Use a set to avoid counting duplicates from overlapping circles.
classSolution {
public:int countLatticePoints(vector<vector<int>>& circles) {
set<pair<int, int>> pts;
for (auto& c : circles) {
int x = c[0], y = c[1], r = c[2];
for (int i = x - r; i <= x + r; ++i) {
for (int j = y - r; j <= y + r; ++j) {
if ((i - x) * (i - x) + (j - y) * (j - y) <= r * r) {
pts.insert({i, j});
}
}
}
}
return pts.size();
}
};
classSolution {
publicintcountLatticePoints(int[][] circles) {
Set<String> pts =new HashSet<>();
for (int[] c : circles) {
int x = c[0], y = c[1], r = c[2];
for (int i = x - r; i <= x + r; ++i) {
for (int j = y - r; j <= y + r; ++j) {
if ((i - x) * (i - x) + (j - y) * (j - y) <= r * r) {
pts.add(i +","+ j);
}
}
}
}
return pts.size();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funcountLatticePoints(circles: Array<IntArray>): Int {
val pts = mutableSetOf<Pair<Int, Int>>()
for (c in circles) {
val(x, y, r) = c
for (i in x - r..x + r) {
for (j in y - r..y + r) {
if ((i - x) * (i - x) + (j - y) * (j - y) <= r * r) {
pts.add(i to j)
}
}
}
}
return pts.size
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defcountLatticePoints(self, circles: list[list[int]]) -> int:
pts: set[tuple[int, int]] = set()
for x, y, r in circles:
for i in range(x - r, x + r +1):
for j in range(y - r, y + r +1):
if (i - x) **2+ (j - y) **2<= r * r:
pts.add((i, j))
return len(pts)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::collections::HashSet;
impl Solution {
pubfncount_lattice_points(circles: Vec<Vec<i32>>) -> i32 {
letmut pts = HashSet::new();
for c in circles {
let (x, y, r) = (c[0], c[1], c[2]);
for i in x - r..=x + r {
for j in y - r..=y + r {
if (i - x).pow(2) + (j - y).pow(2) <= r.pow(2) {
pts.insert((i, j));
}
}
}
}
pts.len() asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
countLatticePoints(circles: number[][]):number {
constpts=newSet<string>();
for (const [x, y, r] ofcircles) {
for (leti=x-r; i<=x+r; ++i) {
for (letj=y-r; j<=y+r; ++j) {
if ((i-x) * (i-x) + (j-y) * (j-y) <=r*r) {
pts.add(`${i},${j}`);
}
}
}
}
returnpts.size;
}
}