|
1 |
| -import math |
2 |
| -from collections import defaultdict |
3 |
| - |
4 | 1 | class Solution:
|
5 | 2 | def numPoints(self, darts: list[list[int]], r: int) -> int:
|
6 | 3 | def countDarts(x, y):
|
7 | 4 | count = 0
|
8 |
| - for dart in darts: |
9 |
| - if math.dist((x, y), (dart[0], dart[1])) <= r + 1e-7: # Add small epsilon for precision issues |
| 5 | + for x1, y1 in darts: |
| 6 | + if dist((x, y), (x1, y1)) <= r + 1e-7: |
10 | 7 | count += 1
|
11 | 8 | return count
|
12 |
| - |
| 9 | + |
13 | 10 | def possibleCenters(x1, y1, x2, y2):
|
14 | 11 | dx, dy = x2 - x1, y2 - y1
|
15 |
| - d = math.sqrt(dx * dx + dy * dy) |
| 12 | + d = sqrt(dx * dx + dy * dy) |
16 | 13 | if d > 2 * r:
|
17 | 14 | return []
|
18 | 15 | mid_x, mid_y = (x1 + x2) / 2, (y1 + y2) / 2
|
19 |
| - dist_to_center = math.sqrt(r * r - (d / 2) * (d / 2)) |
| 16 | + dist_to_center = sqrt(r * r - (d / 2) * (d / 2)) |
20 | 17 | offset_x = dist_to_center * dy / d
|
21 | 18 | offset_y = dist_to_center * -dx / d
|
22 |
| - return [(mid_x + offset_x, mid_y + offset_y), (mid_x - offset_x, mid_y - offset_y)] |
| 19 | + return [ |
| 20 | + (mid_x + offset_x, mid_y + offset_y), |
| 21 | + (mid_x - offset_x, mid_y - offset_y), |
| 22 | + ] |
23 | 23 |
|
24 | 24 | n = len(darts)
|
25 | 25 | max_darts = 1
|
26 |
| - |
| 26 | + |
27 | 27 | for i in range(n):
|
28 | 28 | for j in range(i + 1, n):
|
29 |
| - centers = possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1]) |
| 29 | + centers = possibleCenters( |
| 30 | + darts[i][0], darts[i][1], darts[j][0], darts[j][1] |
| 31 | + ) |
30 | 32 | for center in centers:
|
31 | 33 | max_darts = max(max_darts, countDarts(center[0], center[1]))
|
32 | 34 |
|
33 | 35 | return max_darts
|
34 |
| - |
35 |
| -# Example usage: |
36 |
| -solution = Solution() |
37 |
| -print(solution.numPoints([[-2,0],[2,0],[0,2],[0,-2]], 2)) # Output: 4 |
38 |
| -print(solution.numPoints([[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], 5)) # Output: 5 |
0 commit comments