[LeetCode] Maximum Number of Visible Points

1610. Maximum Number of Visible Points

You are given an array points, an integer angle, and your location, where location = [posx, posy] and points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.

Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction. Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].

You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.

There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.

Return the maximum number of points you can see.

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
26
27
28
class Solution {
public:
int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
vector<double> point;
int equal = 0;
for(auto p : points) {
if(p == location) equal++;
else {
point.push_back(atan2(p[1] - location[1], p[0] - location[0])* 180 / M_PI);
}
}
sort(point.begin(), point.end());
vector<double> appendPoint = point;
for(auto p : point) {
appendPoint.push_back(p + 360);
}
int res = 0;
for(int l = 0, r = 0; r < appendPoint.size(); r++) {
while(appendPoint[r] - appendPoint[l] > angle) {
l++;
}
res = max(res, r - l + 1);
}


return equal + res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/14/PS/LeetCode/maximum-number-of-visible-points/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.