1
+ // Runtime: 251 ms (Top 43.04%) | Memory: 144 MB (Top 24.17%)
1
2
class Solution {
2
3
public int visiblePoints (List <List <Integer >> points , int angle , List <Integer > location ) {
3
4
int overlap = 0 ;
@@ -6,33 +7,33 @@ public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> lo
6
7
if (p .get (0 ) == location .get (0 ) && p .get (1 ) == location .get (1 )) {
7
8
overlap ++;
8
9
} else {
9
- list .add (angle (p .get (1 ) - location .get (1 ),
10
+ list .add (angle (p .get (1 ) - location .get (1 ),
10
11
p .get (0 ) - location .get (0 )));
11
12
}
12
13
}
13
14
Collections .sort (list );
14
15
int max = 0 ;
15
16
int n = list .size ();
16
17
int i2 = 0 ;
17
- // list.get(i1) is first angle leg
18
- // list.get(i2) is second angle leg
18
+ // list.get(i1) is first angle leg
19
+ // list.get(i2) is second angle leg
19
20
for (int i1 = 0 ; i1 < n ; i1 ++) {
20
- // let's grow i1-i2 angle as much as possible
21
- // edge case example: angle = 30, i1 = 350 degrees, i2 = 10 degrees
22
- // edge case handling: allow i2 to circle around and calculate second leg as (360 + list.get(i2 % n))
23
- // then i1 = 350, i2 = 370, delta = 20 degrees < 30 degrees
24
- while ((i2 < n && list .get (i2 ) - list .get (i1 ) <= angle ) ||
21
+ // let's grow i1-i2 angle as much as possible
22
+ // edge case example: angle = 30, i1 = 350 degrees, i2 = 10 degrees
23
+ // edge case handling: allow i2 to circle around and calculate second leg as (360 + list.get(i2 % n))
24
+ // then i1 = 350, i2 = 370, delta = 20 degrees < 30 degrees
25
+ while ((i2 < n && list .get (i2 ) - list .get (i1 ) <= angle ) ||
25
26
(i2 >= n && 360 + list .get (i2 % n ) - list .get (i1 ) <= angle )) {
26
27
i2 ++;
27
28
}
28
- // after i2 went as far as possible away from i1 under allowed limit - check if a new maximum found
29
- max = Math .max (max , i2 -i1 );
29
+ // after i2 went as far as possible away from i1 under allowed limit - check if a new maximum found
30
+ max = Math .max (max , i2 -i1 );
30
31
}
31
32
return max + overlap ;
32
33
}
33
-
34
+
34
35
private double angle (int dy , int dx ) {
35
36
double a = Math .toDegrees (Math .atan2 (dy , dx ));
36
37
return (a < 0 ? a + 360 : a );
37
38
}
38
- }
39
+ }
0 commit comments