forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6df3dbf
commit 6114d4e
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
solution/1400-1499/1453.Maximum Number of Darts Inside of a Circular Dartboard/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import java.util.*; | ||
|
||
public 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; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Solution solution = new Solution(); | ||
System.out.println(solution.numPoints(new int[][]{{-2,0},{2,0},{0,2},{0,-2}}, 2)); // Output: 4 | ||
System.out.println(solution.numPoints(new int[][]{{-3,0},{3,0},{2,6},{5,4},{0,9},{7,8}}, 5)); // Output: 5 | ||
} | ||
} |