Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py sol for Maximum Number of Darts Inside of a Circular Dartboard #4873

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions solutions/platinum/leetcode-1453.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,51 @@ class Solution {
```

</CPPSection>
<PySection>

```py
class Solution:
def numPoints(self, darts: List[List[int]], r: int) -> int:
# Compute the distances between all pairs of points
dist = [[0] * len(darts) for _ in range(len(darts))]
for i in range(len(darts)):
for j in range(i + 1, len(darts)):
dx = darts[i][0] - darts[j][0]
dy = darts[i][1] - darts[j][1]
dist[i][j] = dist[j][i] = math.sqrt(dx * dx + dy * dy)

ans = 1
for i in range(len(darts)):
# Store the angles of other points relative to darts[i]
angles = []
for j in range(len(darts)):
# Continue if it's the same point or if it lies outside any circle
if i == j or dist[i][j] > 2 * r:
continue

a = math.atan2(darts[j][1] - darts[i][1], darts[j][0] - darts[i][0])
b = math.acos(dist[i][j] / (2.0 * r))
alpha = a - b
beta = a + b

# The angle at which the point enters the circle
angles.append((alpha, False))
# The angle at which the point leaves the circle
angles.append((beta, True))

# Sort all the angles
angles.sort()

active_points = 1
for angle, is_exit in angles:
if not is_exit:
active_points += 1
else:
active_points -= 1
ans = max(ans, active_points)

return ans
```

</PySection>
</LanguageSection>
3 changes: 1 addition & 2 deletions solutions/platinum/leetcode-149.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
id: leetcode-149
source: LC
title: Max Points on a Line
author: Mihnea Brebenel
contributors: Rameez Parwez
author: Mihnea Brebenel, Rameez Parwez
---

## Explanation
Expand Down
Loading