2249. Count Lattice Points Inside a Circle

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int countLatticePoints(vector<vector<int>>& circles) {
set<pair<int,int>> s;
for(vector<int> &circle: circles){
int ox = circle[0], oy = circle[1], r = circle[2];
int lx = ox - r;
int rx = ox + r;
int ly = oy - r;
int ry = oy + r;
for(int x = lx;x<=rx;++x){
for(int y = ly;y<=ry ; ++y){
// check
if(pow((x-ox),2) +pow( (y-oy),2)<=r*r){
s.insert({x,y});
}
}
}
}
return s.size();
}
};

analysis

  • time complexity O(nxy)
  • space complexity O(1)