Skip to content

Commit a3a1694

Browse files
authored
Update Solution.py
1 parent 1d37bce commit a3a1694

File tree

1 file changed

+13
-16
lines changed
  • solution/1400-1499/1453.Maximum Number of Darts Inside of a Circular Dartboard

1 file changed

+13
-16
lines changed
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1-
import math
2-
from collections import defaultdict
3-
41
class Solution:
52
def numPoints(self, darts: list[list[int]], r: int) -> int:
63
def countDarts(x, y):
74
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:
107
count += 1
118
return count
12-
9+
1310
def possibleCenters(x1, y1, x2, y2):
1411
dx, dy = x2 - x1, y2 - y1
15-
d = math.sqrt(dx * dx + dy * dy)
12+
d = sqrt(dx * dx + dy * dy)
1613
if d > 2 * r:
1714
return []
1815
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))
2017
offset_x = dist_to_center * dy / d
2118
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+
]
2323

2424
n = len(darts)
2525
max_darts = 1
26-
26+
2727
for i in range(n):
2828
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+
)
3032
for center in centers:
3133
max_darts = max(max_darts, countDarts(center[0], center[1]))
3234

3335
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

Comments
 (0)