Skip to content

Commit 318f56b

Browse files
committed
1610. Maximum Number of Visible Points: AC
1 parent 92f1ed8 commit 318f56b

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1214,3 +1214,4 @@ mod s1605_find_valid_matrix_given_row_and_column_sums;
12141214
mod s1606_find_servers_that_handled_most_number_of_requests;
12151215
mod s1608_special_array_with_x_elements_greater_than_or_equal_x;
12161216
mod s1609_even_odd_tree;
1217+
mod s1610_maximum_number_of_visible_points;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/**
2+
* [1610] Maximum Number of Visible Points
3+
*
4+
* You are given an array points, an integer angle, and your location, where location = [posx, posy] and points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.
5+
* Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction. Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].
6+
*
7+
* <video autoplay="" controls="" height="360" muted="" style="max-width:100%;height:auto;" width="480"><source src="https://assets.leetcode.com/uploads/2020/09/30/angle.mp4" type="video/mp4" />Your browser does not support the video tag or this video format.</video>
8+
*
9+
* You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.
10+
* There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.
11+
* Return the maximum number of points you can see.
12+
*
13+
* Example 1:
14+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/30/89a07e9b-00ab-4967-976a-c723b2aa8656.png" style="width: 400px; height: 300px;" />
15+
* Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
16+
* Output: 3
17+
* Explanation: The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.
18+
*
19+
* Example 2:
20+
*
21+
* Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
22+
* Output: 4
23+
* Explanation: All points can be made visible in your field of view, including the one at your location.
24+
*
25+
* Example 3:
26+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/30/5010bfd3-86e6-465f-ac64-e9df941d2e49.png" style="width: 690px; height: 348px;" />
27+
* Input: points = [[1,0],[2,1]], angle = 13, location = [1,1]
28+
* Output: 1
29+
* Explanation: You can only see one of the two points, as shown above.
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* 1 <= points.length <= 10^5
35+
* points[i].length == 2
36+
* location.length == 2
37+
* 0 <= angle < 360
38+
* 0 <= posx, posy, xi, yi <= 100
39+
*
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.com/problems/maximum-number-of-visible-points/
44+
// discuss: https://leetcode.com/problems/maximum-number-of-visible-points/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn visible_points(points: Vec<Vec<i32>>, angle: i32, location: Vec<i32>) -> i32 {
50+
if points.len() == 0 {
51+
return 0;
52+
}
53+
let mut extra = 0;
54+
let mut angles = points
55+
.iter()
56+
.filter(|p| {
57+
if p[0] == location[0] && p[1] == location[1] {
58+
extra += 1;
59+
false
60+
} else {
61+
true
62+
}
63+
})
64+
.map(|p| ((location[1] - p[1]) as f64).atan2((location[0] - p[0]) as f64))
65+
.map(|a| {
66+
360.0
67+
* (if a < 0.0 {
68+
a + 2.0 * std::f64::consts::PI
69+
} else {
70+
a
71+
})
72+
/ (2.0 * std::f64::consts::PI)
73+
})
74+
.collect::<Vec<_>>();
75+
76+
for i in 0..angles.len() {
77+
angles.push(angles[i] + 360.0);
78+
}
79+
80+
if angles.len() == 0 {
81+
return extra as i32;
82+
}
83+
84+
angles.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
85+
86+
let angle = angle as f64;
87+
let mut best = 1;
88+
let mut l = 0;
89+
let mut r = 1;
90+
91+
while r < angles.len() && l < angles.len() {
92+
while r < angles.len() && angles[r] - angles[l] <= angle {
93+
r += 1;
94+
}
95+
best = best.max(r - l);
96+
l += 1;
97+
}
98+
99+
(extra + best) as i32
100+
}
101+
}
102+
103+
// submission codes end
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use super::*;
108+
109+
#[test]
110+
fn test_1610_example_1() {
111+
let points = vec![vec![2, 1], vec![2, 2], vec![3, 3]];
112+
let angle = 90;
113+
let location = vec![1, 1];
114+
115+
let result = 3;
116+
117+
assert_eq!(Solution::visible_points(points, angle, location), result);
118+
}
119+
120+
#[test]
121+
fn test_1610_example_2() {
122+
let points = vec![vec![2, 1], vec![2, 2], vec![3, 4], vec![1, 1]];
123+
let angle = 90;
124+
let location = vec![1, 1];
125+
126+
let result = 4;
127+
128+
assert_eq!(Solution::visible_points(points, angle, location), result);
129+
}
130+
131+
#[test]
132+
fn test_1610_example_3() {
133+
let points = vec![vec![1, 0], vec![2, 1]];
134+
let angle = 13;
135+
let location = vec![1, 1];
136+
137+
let result = 1;
138+
139+
assert_eq!(Solution::visible_points(points, angle, location), result);
140+
}
141+
}

0 commit comments

Comments
 (0)