Alice is throwing n darts on a very large wall. You are given an array darts where darts[i] = [xi, yi] is the position of the ith dart that Alice threw on the wall.
Bob knows the positions of the n darts on the wall. He wants to place a dartboard of radius r on the wall so that the maximum number of darts that Alice throws lie on the dartboard.
Given the integer r, return the maximum number of darts that can lie on the dartboard.
Input: darts =[[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r =5Output: 5Explanation: Circle dartboard with center in(0,4) and radius =5 contain all points except the point(7,8).
To maximize the number of darts inside the dartboard, consider every pair of darts as possible candidates for the circle’s boundary. For each pair, compute all possible circle centers of radius r passing through both points, then count how many darts fall inside or on the circle. The answer is the maximum count found.
classSolution {
funnumPoints(darts: Array<IntArray>, r: Int): Int {
val n = darts.size
var ans = 1for (i in0 until n) {
for (j in i+1 until n) {
val dx = darts[j][0] - darts[i][0]
val dy = darts[j][1] - darts[i][1]
val d = Math.hypot(dx.toDouble(), dy.toDouble())
if (d > 2*r) continueval mx = (darts[i][0] + darts[j][0]) / 2.0val my = (darts[i][1] + darts[j][1]) / 2.0val h = Math.sqrt(r.toDouble()*r - (d/2)*(d/2))
val ox = -dy * h / d
val oy = dx * h / d
for (sign in listOf(-1, 1)) {
val cx = mx + sign * ox
val cy = my + sign * oy
var cnt = 0for (k in0 until n) {
val ddx = darts[k][0] - cx
val ddy = darts[k][1] - cy
if (ddx*ddx + ddy*ddy <= r*r + 1e-8) cnt++ }
ans = maxOf(ans, cnt)
}
}
}
return ans
}
}
defnumPoints(darts: list[list[int]], r: int) -> int:
from math import hypot, sqrt
n, ans = len(darts), 1for i in range(n):
for j in range(i+1, n):
dx, dy = darts[j][0] - darts[i][0], darts[j][1] - darts[i][1]
d = hypot(dx, dy)
if d >2*r: continue mx, my = (darts[i][0] + darts[j][0]) /2, (darts[i][1] + darts[j][1]) /2 h = sqrt(r*r - (d/2)*(d/2))
ox, oy =-dy * h / d, dx * h / d
for sign in [-1, 1]:
cx, cy = mx + sign*ox, my + sign*oy
cnt =0for k in range(n):
ddx, ddy = darts[k][0] - cx, darts[k][1] - cy
if ddx*ddx + ddy*ddy <= r*r +1e-8:
cnt +=1 ans = max(ans, cnt)
return ans
impl Solution {
pubfnnum_points(darts: Vec<Vec<i32>>, r: i32) -> i32 {
let n = darts.len();
letmut ans =1;
for i in0..n {
for j in i+1..n {
let dx = (darts[j][0] - darts[i][0]) asf64;
let dy = (darts[j][1] - darts[i][1]) asf64;
let d = (dx*dx + dy*dy).sqrt();
if d >2.0* r asf64 { continue; }
let mx = (darts[i][0] + darts[j][0]) asf64/2.0;
let my = (darts[i][1] + darts[j][1]) asf64/2.0;
let h = ((r*r) asf64- (d/2.0)*(d/2.0)).sqrt();
let ox =-dy * h / d;
let oy = dx * h / d;
for&sign in&[-1.0, 1.0] {
let cx = mx + sign * ox;
let cy = my + sign * oy;
letmut cnt =0;
for k in0..n {
let ddx = darts[k][0] asf64- cx;
let ddy = darts[k][1] asf64- cy;
if ddx*ddx + ddy*ddy <= (r*r) asf64+1e-8 {
cnt +=1;
}
}
ans = ans.max(cnt);
}
}
}
ans
}
}