Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1453 (#3395)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodersAcademy006 authored Aug 13, 2024
1 parent d6aa474 commit 248c0a7
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,92 @@ tags:
#### Python3

```python

class Solution:
def numPoints(self, darts: list[list[int]], r: int) -> int:
def countDarts(x, y):
count = 0
for x1, y1 in darts:
if dist((x, y), (x1, y1)) <= r + 1e-7:
count += 1
return count

def possibleCenters(x1, y1, x2, y2):
dx, dy = x2 - x1, y2 - y1
d = sqrt(dx * dx + dy * dy)
if d > 2 * r:
return []
mid_x, mid_y = (x1 + x2) / 2, (y1 + y2) / 2
dist_to_center = sqrt(r * r - (d / 2) * (d / 2))
offset_x = dist_to_center * dy / d
offset_y = dist_to_center * -dx / d
return [
(mid_x + offset_x, mid_y + offset_y),
(mid_x - offset_x, mid_y - offset_y),
]

n = len(darts)
max_darts = 1

for i in range(n):
for j in range(i + 1, n):
centers = possibleCenters(
darts[i][0], darts[i][1], darts[j][0], darts[j][1]
)
for center in centers:
max_darts = max(max_darts, countDarts(center[0], center[1]))

return max_darts
```

#### Java

```java

class Solution {
public int numPoints(int[][] darts, int r) {
int n = darts.length;
int maxDarts = 1;

for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
List<double[]> centers
= possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1], r);
for (double[] center : centers) {
maxDarts = Math.max(maxDarts, countDarts(center[0], center[1], darts, r));
}
}
}
return maxDarts;
}

private List<double[]> possibleCenters(int x1, int y1, int x2, int y2, int r) {
List<double[]> centers = new ArrayList<>();
double dx = x2 - x1;
double dy = y2 - y1;
double d = Math.sqrt(dx * dx + dy * dy);
if (d > 2 * r) {
return centers;
}
double midX = (x1 + x2) / 2.0;
double midY = (y1 + y2) / 2.0;
double distToCenter = Math.sqrt(r * r - (d / 2.0) * (d / 2.0));
double offsetX = distToCenter * dy / d;
double offsetY = distToCenter * -dx / d;

centers.add(new double[] {midX + offsetX, midY + offsetY});
centers.add(new double[] {midX - offsetX, midY - offsetY});
return centers;
}

private int countDarts(double x, double y, int[][] darts, int r) {
int count = 0;
for (int[] dart : darts) {
if (Math.sqrt(Math.pow(dart[0] - x, 2) + Math.pow(dart[1] - y, 2)) <= r + 1e-7) {
count++;
}
}
return count;
}
}
```

#### C++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,99 @@ tags:

<!-- solution:start -->

### Solution 1
### 方法一

<!-- tabs:start -->

#### Python3

```python

class Solution:
def numPoints(self, darts: list[list[int]], r: int) -> int:
def countDarts(x, y):
count = 0
for x1, y1 in darts:
if dist((x, y), (x1, y1)) <= r + 1e-7:
count += 1
return count

def possibleCenters(x1, y1, x2, y2):
dx, dy = x2 - x1, y2 - y1
d = sqrt(dx * dx + dy * dy)
if d > 2 * r:
return []
mid_x, mid_y = (x1 + x2) / 2, (y1 + y2) / 2
dist_to_center = sqrt(r * r - (d / 2) * (d / 2))
offset_x = dist_to_center * dy / d
offset_y = dist_to_center * -dx / d
return [
(mid_x + offset_x, mid_y + offset_y),
(mid_x - offset_x, mid_y - offset_y),
]

n = len(darts)
max_darts = 1

for i in range(n):
for j in range(i + 1, n):
centers = possibleCenters(
darts[i][0], darts[i][1], darts[j][0], darts[j][1]
)
for center in centers:
max_darts = max(max_darts, countDarts(center[0], center[1]))

return max_darts
```

#### Java

```java

class Solution {
public int numPoints(int[][] darts, int r) {
int n = darts.length;
int maxDarts = 1;

for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
List<double[]> centers
= possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1], r);
for (double[] center : centers) {
maxDarts = Math.max(maxDarts, countDarts(center[0], center[1], darts, r));
}
}
}
return maxDarts;
}

private List<double[]> possibleCenters(int x1, int y1, int x2, int y2, int r) {
List<double[]> centers = new ArrayList<>();
double dx = x2 - x1;
double dy = y2 - y1;
double d = Math.sqrt(dx * dx + dy * dy);
if (d > 2 * r) {
return centers;
}
double midX = (x1 + x2) / 2.0;
double midY = (y1 + y2) / 2.0;
double distToCenter = Math.sqrt(r * r - (d / 2.0) * (d / 2.0));
double offsetX = distToCenter * dy / d;
double offsetY = distToCenter * -dx / d;

centers.add(new double[] {midX + offsetX, midY + offsetY});
centers.add(new double[] {midX - offsetX, midY - offsetY});
return centers;
}

private int countDarts(double x, double y, int[][] darts, int r) {
int count = 0;
for (int[] dart : darts) {
if (Math.sqrt(Math.pow(dart[0] - x, 2) + Math.pow(dart[1] - y, 2)) <= r + 1e-7) {
count++;
}
}
return count;
}
}
```

#### C++
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution {
public int numPoints(int[][] darts, int r) {
int n = darts.length;
int maxDarts = 1;

for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
List<double[]> centers
= possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1], r);
for (double[] center : centers) {
maxDarts = Math.max(maxDarts, countDarts(center[0], center[1], darts, r));
}
}
}
return maxDarts;
}

private List<double[]> possibleCenters(int x1, int y1, int x2, int y2, int r) {
List<double[]> centers = new ArrayList<>();
double dx = x2 - x1;
double dy = y2 - y1;
double d = Math.sqrt(dx * dx + dy * dy);
if (d > 2 * r) {
return centers;
}
double midX = (x1 + x2) / 2.0;
double midY = (y1 + y2) / 2.0;
double distToCenter = Math.sqrt(r * r - (d / 2.0) * (d / 2.0));
double offsetX = distToCenter * dy / d;
double offsetY = distToCenter * -dx / d;

centers.add(new double[] {midX + offsetX, midY + offsetY});
centers.add(new double[] {midX - offsetX, midY - offsetY});
return centers;
}

private int countDarts(double x, double y, int[][] darts, int r) {
int count = 0;
for (int[] dart : darts) {
if (Math.sqrt(Math.pow(dart[0] - x, 2) + Math.pow(dart[1] - y, 2)) <= r + 1e-7) {
count++;
}
}
return count;
}
}

0 comments on commit 248c0a7

Please sign in to comment.