-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathmaximum-number-of-visible-points.cpp
More file actions
33 lines (31 loc) · 991 Bytes
/
maximum-number-of-visible-points.cpp
File metadata and controls
33 lines (31 loc) · 991 Bytes
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
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
static const double PI = atan2(0, -1);
vector<double> arr;
int extra = 0;
for (const auto& p : points) {
if (p == location) {
++extra;
continue;
}
arr.emplace_back(atan2(p[1] - location[1], p[0] - location[0]));
}
sort(begin(arr), end(arr));
const int n = size(arr);
for (int i = 0; i < n; ++i) { // make it circular
arr.emplace_back(arr[i] + 2.0 * PI);
}
const double d = 2.0 * PI * (angle / 360.0);
int result = 0;
for (int left = 0, right = 0; right < size(arr); ++right) {
while (arr[right] - arr[left] > d) {
++left;
}
result = max(result, right - left + 1);
}
return result + extra;
}
};