Skip to content

Commit 7a1c4cd

Browse files
committed
Add solution #1610
1 parent 496957d commit 7a1c4cd

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,418 LeetCode solutions in JavaScript
1+
# 1,419 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1242,6 +1242,7 @@
12421242
1605|[Find Valid Matrix Given Row and Column Sums](./solutions/1605-find-valid-matrix-given-row-and-column-sums.js)|Medium|
12431243
1608|[Special Array With X Elements Greater Than or Equal X](./solutions/1608-special-array-with-x-elements-greater-than-or-equal-x.js)|Easy|
12441244
1609|[Even Odd Tree](./solutions/1609-even-odd-tree.js)|Medium|
1245+
1610|[Maximum Number of Visible Points](./solutions/1610-maximum-number-of-visible-points.js)|Hard|
12451246
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12461247
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12471248
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* 1610. Maximum Number of Visible Points
3+
* https://leetcode.com/problems/maximum-number-of-visible-points/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array points, an integer angle, and your location, where location = [posx, posy]
7+
* and points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.
8+
*
9+
* Initially, you are facing directly east from your position. You cannot move from your position,
10+
* but you can rotate. In other words, posx and posy cannot be changed. Your field of view in
11+
* degrees is represented by angle, determining how wide you can see from any given view direction.
12+
* Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the
13+
* inclusive range of angles [d - angle/2, d + angle/2].
14+
*
15+
* You can see some set of points if, for each point, the angle formed by the point, your position,
16+
* and the immediate east direction from your position is in your field of view.
17+
*
18+
* There can be multiple points at one coordinate. There may be points at your location, and you
19+
* can always see these points regardless of your rotation. Points do not obstruct your vision to
20+
* other points.
21+
*
22+
* Return the maximum number of points you can see.
23+
*/
24+
25+
/**
26+
* @param {number[][]} points
27+
* @param {number} angle
28+
* @param {number[]} location
29+
* @return {number}
30+
*/
31+
var visiblePoints = function(points, angle, location) {
32+
const angles = [];
33+
let originPoints = 0;
34+
const [x0, y0] = location;
35+
36+
for (const [x, y] of points) {
37+
if (x === x0 && y === y0) {
38+
originPoints++;
39+
continue;
40+
}
41+
const radian = Math.atan2(y - y0, x - x0);
42+
const degree = (radian * 180) / Math.PI;
43+
angles.push(degree);
44+
angles.push(degree + 360);
45+
}
46+
47+
angles.sort((a, b) => a - b);
48+
const threshold = angle;
49+
let maxVisible = 0;
50+
let left = 0;
51+
52+
for (let right = 0; right < angles.length; right++) {
53+
while (angles[right] - angles[left] > threshold) {
54+
left++;
55+
}
56+
maxVisible = Math.max(maxVisible, right - left + 1);
57+
}
58+
59+
return maxVisible + originPoints;
60+
};

0 commit comments

Comments
 (0)