Skip to content

Commit 93bf412

Browse files
authored
Create maximum-number-of-visible-points.cpp
1 parent cdf98f9 commit 93bf412

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
7+
static const double PI = atan2(0, -1);
8+
9+
vector<double> arr;
10+
int extra = 0;
11+
for (const auto& p : points) {
12+
if (p == location) {
13+
++extra;
14+
continue;
15+
}
16+
arr.emplace_back(atan2(p[1] - location[1], p[0] - location[0]));
17+
}
18+
sort(begin(arr), end(arr));
19+
const int n = size(arr);
20+
for (int i = 0; i < n; ++i) { // make it circular
21+
arr.emplace_back(arr[i] + 2.0 * PI);
22+
}
23+
const double d = 2.0 * PI * (angle / 360.0);
24+
int result = 0;
25+
for (int left = 0, right = 0; right < size(arr); ++right) {
26+
while (arr[right] - arr[left] > d) {
27+
++left;
28+
}
29+
result = max(result, right - left + 1);
30+
}
31+
return result + extra;
32+
}
33+
};

0 commit comments

Comments
 (0)