File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
solution/1400-1499/1453.Maximum Number of Darts Inside of a Circular Dartboard Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numPoints (self , darts : list [list [int ]], r : int ) -> int :
3
+ def countDarts (x , y ):
4
+ count = 0
5
+ for x1 , y1 in darts :
6
+ if dist ((x , y ), (x1 , y1 )) <= r + 1e-7 :
7
+ count += 1
8
+ return count
9
+
10
+ def possibleCenters (x1 , y1 , x2 , y2 ):
11
+ dx , dy = x2 - x1 , y2 - y1
12
+ d = sqrt (dx * dx + dy * dy )
13
+ if d > 2 * r :
14
+ return []
15
+ mid_x , mid_y = (x1 + x2 ) / 2 , (y1 + y2 ) / 2
16
+ dist_to_center = sqrt (r * r - (d / 2 ) * (d / 2 ))
17
+ offset_x = dist_to_center * dy / d
18
+ offset_y = dist_to_center * - dx / d
19
+ return [
20
+ (mid_x + offset_x , mid_y + offset_y ),
21
+ (mid_x - offset_x , mid_y - offset_y ),
22
+ ]
23
+
24
+ n = len (darts )
25
+ max_darts = 1
26
+
27
+ for i in range (n ):
28
+ for j in range (i + 1 , n ):
29
+ centers = possibleCenters (
30
+ darts [i ][0 ], darts [i ][1 ], darts [j ][0 ], darts [j ][1 ]
31
+ )
32
+ for center in centers :
33
+ max_darts = max (max_darts , countDarts (center [0 ], center [1 ]))
34
+
35
+ return max_darts
You can’t perform that action at this time.
0 commit comments