|
| 1 | +import math |
| 2 | +from collections import defaultdict |
| 3 | + |
| 4 | +class Solution: |
| 5 | + def numPoints(self, darts: list[list[int]], r: int) -> int: |
| 6 | + def countDarts(x, y): |
| 7 | + 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 |
| 10 | + count += 1 |
| 11 | + return count |
| 12 | + |
| 13 | + def possibleCenters(x1, y1, x2, y2): |
| 14 | + dx, dy = x2 - x1, y2 - y1 |
| 15 | + d = math.sqrt(dx * dx + dy * dy) |
| 16 | + if d > 2 * r: |
| 17 | + return [] |
| 18 | + mid_x, mid_y = (x1 + x2) / 2, (y1 + y2) / 2 |
| 19 | + dist_to_center = math.sqrt(r * r - (d / 2) * (d / 2)) |
| 20 | + offset_x = dist_to_center * dy / d |
| 21 | + 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)] |
| 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(darts[i][0], darts[i][1], darts[j][0], darts[j][1]) |
| 30 | + for center in centers: |
| 31 | + max_darts = max(max_darts, countDarts(center[0], center[1])) |
| 32 | + |
| 33 | + 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