Skip to content

Commit aaaf7bd

Browse files
authored
Create 1610-maximum-number-of-visible-points.js
1 parent 981a7df commit aaaf7bd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[][]} points
3+
* @param {number} angle
4+
* @param {number[]} location
5+
* @return {number}
6+
*/
7+
const visiblePoints = function (points, angle, location) {
8+
const angles = [];
9+
let count = 0;
10+
for (let p of points) {
11+
let dx = p[0] - location[0];
12+
let dy = p[1] - location[1];
13+
if (dx == 0 && dy == 0) {
14+
// edge case of same point
15+
count++;
16+
continue;
17+
}
18+
angles.push(Math.atan2(dy, dx) * (180 / Math.PI));
19+
}
20+
angles.sort();
21+
const tmp = angles.slice();
22+
for (let d of angles) tmp.push(d + 360); // concatenate to handle edge case
23+
let res = count;
24+
for (let i = 0, j = 0; i < tmp.length; i++) {
25+
while (tmp[i] - tmp[j] > angle) {
26+
j++;
27+
}
28+
res = Math.max(res, count + i - j + 1);
29+
}
30+
return res;
31+
};

0 commit comments

Comments
 (0)