Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6aa474

Browse files
feat: add python solution to lc problem: No.1453 (doocs#3394)
1 parent 72947dc commit d6aa474

File tree

1 file changed

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

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /