Closed antop-dev closed 3 months ago
각 원마다 안에 포함될 수 있는 점을 먼저 필터링(녹색+파란색) 한 후, 원의 중심점과의 거리가 반지름보다 짧으면(파란색) OK!
class Solution {
fun countPoints(points: Array<IntArray>, queries: Array<IntArray>): IntArray {
return IntArray(queries.size) { i ->
val (px, py, r) = queries[i]
count(px, py, r, points)
}
}
private fun count(qx: Int, qy: Int, radius: Int, points: Array<IntArray>): Int {
return points.count { (px, py) ->
px in qx - radius..qx + radius
&& py in qy - radius..qy + radius
&& distance(qx, qy, px, py) <= radius
}
}
// 두 점의 거리 구하기
private fun distance(qx: Int, qy: Int, px: Int, py: Int): Double {
val xDist = px.toDouble() - qx
val yDist = py.toDouble() - qy
return sqrt(xDist * xDist + yDist * yDist)
}
}
파이썬 풀이
class Solution:
def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
ans = [0] * len(queries)
for i in range(len(queries)):
px, py, r = queries[i]
ans[i] = self.count(px, py, r, points)
return ans
def count(self, qx, qy, r, points):
count = 0
for px, py in points:
if qx - r <= px <= qx + r \
and qy - r <= py <= qy + r \
and self.distance(qx, qy, px, py) <= r:
count += 1
return count
def distance(self, qx, qy, px, py):
x_dist = px - qx
y_dist = py - qy
return math.sqrt(x_dist ** 2 + y_dist ** 2)
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/