1453. Java의 Leetcode 솔루션
2731 단어 java
class Solution {
public int numPoints(int[][] points, int r) {
int maxPoints = 1;
int pointsCount = points.length;
for (int i = 0; i < pointsCount; i++) {
for (int j = i + 1; j < pointsCount; j++) {
double[][] intersections = getIntersections(points[i], points[j], r);
for (double[] intersection : intersections) {
int pointsInCircle = 0;
for (int[] point : points) {
double distance = distance(intersection, point);
if (distance <= r + 1e-5)
pointsInCircle++;
}
maxPoints = Math.max(maxPoints, pointsInCircle);
}
}
}
return maxPoints;
}
public double[][] getIntersections(int[] point1, int[] point2, int radius) {
int squaredDistance = squaredDistance(point1, point2);
if (squaredDistance > radius * radius * 4)
return new double[0][2];
else if (squaredDistance == radius * radius * 4) {
double[] intersection = new double[2];
for (int i = 0; i < 2; i++)
intersection[i] = (point1[i] + point2[i]) / 2.0;
double[][] intersections = new double[1][2];
intersections[0] = intersection;
return intersections;
} else {
double[] midPoint = new double[2];
for (int i = 0; i < 2; i++)
midPoint[i] = (point1[i] + point2[i]) / 2.0;
double remaining = Math.sqrt(radius * radius - squaredDistance / 4.0);
int difference1 = point1[1] - point2[1];
int difference2 = point2[0] - point1[0];
double radian = Math.atan(1.0 * difference2 / difference1);
double[] intersection0 = {midPoint[0] + remaining * Math.cos(radian), midPoint[1] + remaining * Math.sin(radian)};
double[] intersection1 = {midPoint[0] - remaining * Math.cos(radian), midPoint[1] - remaining * Math.sin(radian)};
double[][] intersections = new double[2][2];
intersections[0] = intersection0;
intersections[1] = intersection1;
return intersections;
}
}
public int squaredDistance(int[] point1, int[] point2) {
return (point2[0] - point1[0]) * (point2[0] - point1[0]) + (point2[1] - point1[1]) * (point2[1] - point1[1]);
}
public double distance(double[] point1, int[] point2) {
return Math.sqrt((point2[0] - point1[0]) * (point2[0] - point1[0]) + (point2[1] - point1[1]) * (point2[1] - point1[1]));
}
}
Reference
이 문제에 관하여(1453. Java의 Leetcode 솔루션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/chiki1601/1453-leetcode-solution-in-java-316d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)