-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1610. Maximum Number of Visible Points
45 lines (30 loc) · 1.21 KB
/
1610. Maximum Number of Visible Points
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
List<Double> angles = new ArrayList<>();
int sameLocationPoints = 0;
for (List<Integer> point : points) {
int dx = point.get(0) - location.get(0);
int dy = point.get(1) - location.get(1);
if (dx == 0 && dy == 0) {
sameLocationPoints++;
continue;
}
double angleInDegrees = Math.toDegrees(Math.atan2(dy, dx));
angles.add(angleInDegrees);
}
Collections.sort(angles);
int n = angles.size();
for (int i = 0; i < n; i++) {
angles.add(angles.get(i) + 360);
}
int maxPoints = 0;
int left = 0;
for (int right = 0; right < angles.size(); right++) {
while (angles.get(right) - angles.get(left) > angle) {
left++;
}
maxPoints = Math.max(maxPoints, right - left + 1);
}
return maxPoints + sameLocationPoints;
}
}