From d586e39bc2783ad0683f4991f28e5107e81991e8 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Mon, 28 Oct 2024 19:01:39 +0530 Subject: [PATCH 1/3] py sol for Maximum Number of Darts Inside of a Circular Dartboard --- solutions/platinum/leetcode-1453.mdx | 47 ++++++++++++++++++++++++++++ solutions/platinum/leetcode-149.mdx | 3 +- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/solutions/platinum/leetcode-1453.mdx b/solutions/platinum/leetcode-1453.mdx index e3caa98b07..d2061cc90a 100644 --- a/solutions/platinum/leetcode-1453.mdx +++ b/solutions/platinum/leetcode-1453.mdx @@ -104,4 +104,51 @@ class Solution { ``` + + +```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 +``` + + diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index ca8dd39a4a..fd987cff97 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -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 From 0ef79521b2efbec3999d3f3f9a21fb370c30226a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:33:47 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/platinum/leetcode-1453.mdx | 80 ++++++++++++++-------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/solutions/platinum/leetcode-1453.mdx b/solutions/platinum/leetcode-1453.mdx index d2061cc90a..1d3fd4d977 100644 --- a/solutions/platinum/leetcode-1453.mdx +++ b/solutions/platinum/leetcode-1453.mdx @@ -108,46 +108,46 @@ class Solution { ```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 + 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 ``` From 547d6af186aaec9bb18bcce298a7b590b2731fd2 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:02:45 -0700 Subject: [PATCH 3/3] Update leetcode-1453.mdx --- solutions/platinum/leetcode-1453.mdx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/solutions/platinum/leetcode-1453.mdx b/solutions/platinum/leetcode-1453.mdx index 1d3fd4d977..cf69c24bf3 100644 --- a/solutions/platinum/leetcode-1453.mdx +++ b/solutions/platinum/leetcode-1453.mdx @@ -89,7 +89,7 @@ class Solution { int active_points = 1; for (const pair &angle : angles) { - if (angle.second == false) { + if (!angle.second) { active_points++; } else { active_points--; @@ -141,10 +141,7 @@ class Solution: active_points = 1 for angle, is_exit in angles: - if not is_exit: - active_points += 1 - else: - active_points -= 1 + active_points += -1 if is_exit else 1 ans = max(ans, active_points) return ans