[LeetCode] Count Lattice Points Inside a Circle

2249. Count Lattice Points Inside a Circle

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
bool include(vector<int>& c, int y, int x) {
if(pow(c[2], 2) >= pow(c[0] - x, 2) + pow(c[1] - y, 2))
return true;
return false;
}
public:
int countLatticePoints(vector<vector<int>>& circles) {
int res = 0, mi = INT_MAX, ma = INT_MIN;
for(auto& c : circles) {
mi = min({mi, -c[2] + c[0], -c[2] + c[1]});
ma = max({ma, c[2] + c[0], c[2] + c[1]});
}
for(int i = mi; i <= ma; i++) {
for(int j = mi; j <= ma; j++) {
bool inc = false;
for(int k = 0; k < circles.size() and !inc; k++) {
inc = include(circles[k],i,j);
}
res += inc;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/24/PS/LeetCode/count-lattice-points-inside-a-circle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.