Maximum Number of Visible Points LeetCode solution in C++

1
class Solution {
2
public:
3
int visiblePoints(vector<vector<int>> &points, int angle, vector<int> &location) {
4
vector<double> pa;
5
int at_start = 0;
6
for (auto point : points) {
7
if (point[0] == location[0] && point[1] == location[1])
8
at_start++;
9
else {
10
double ang = atan2(point[1] - location[1], point[0] - location[0]) * 180.0 / M_PI;
11
if (ang < 0) ang += 360;
12
pa.push_back(ang);
13
}
14
}
15
sort(pa.begin(), pa.end());
16
int n = pa.size();
17
int l = 0, ret = 0;
18
for (int i = 0; i < n; i++) pa.push_back(pa[i] + 360);
19
for (int r = 0; r < pa.size(); r++) {
20
while ((pa[r] - pa[l]) > angle) l++;
21
ret = max(ret, r - l + 1);
22
}
23
return ret + at_start;
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0