1828. 统计一个圆中点的数目
给你一个数组 points
,其中 points[i] = [xi, yi]
,表示第 i
个点在二维平面上的坐标。多个点可能会有 相同 的坐标。
同时给你一个数组 queries
,其中 queries[j] = [xj, yj, rj]
,表示一个圆心在 (xj, yj)
且半径为 rj
的圆。
对于每一个查询 queries[j]
,计算在第 j
个圆 内 点的数目。如果一个点在圆的 边界上 ,我们同样认为它在圆 内 。
请你返回一个数组 answer
,其中 answer[j]
是第 j
个查询的答案。
示例 1:
输入:points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]] 输出:[3,2,2] 解释:所有的点和圆如上图所示。 queries[0] 是绿色的圆,queries[1] 是红色的圆,queries[2] 是蓝色的圆。
示例 2:
输入:points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]] 输出:[2,3,2,4] 解释:所有的点和圆如上图所示。 queries[0] 是绿色的圆,queries[1] 是红色的圆,queries[2] 是蓝色的圆,queries[3] 是紫色的圆。
提示:
1 <= points.length <= 500
points[i].length == 2
0 <= xi, yi <= 500
1 <= queries.length <= 500
queries[j].length == 3
0 <= xj, yj <= 500
1 <= rj <= 500
原站题解
elixir 解法, 执行用时: 980 ms, 内存消耗: 59.1 MB, 提交时间: 2023-09-24 23:48:09
defmodule Solution do @spec count_points(points :: [[integer]], queries :: [[integer]]) :: [integer] def count_points(points, queries) do Enum.map(queries, fn x -> for point <- points do isOK(point, x) end |> Enum.filter(&(&1 == true)) |> Enum.count() end) end defp isOK(a, b) do Enum.at(b, 2) * Enum.at(b, 2) >= (Enum.at(a, 0) - Enum.at(b, 0)) * (Enum.at(a, 0) - Enum.at(b, 0)) + (Enum.at(a, 1) - Enum.at(b, 1)) * (Enum.at(a, 1) - Enum.at(b, 1)) end end
erlang 解法, 执行用时: 224 ms, 内存消耗: 47.6 MB, 提交时间: 2023-09-24 23:47:45
-spec count_points(Points :: [[integer()]], Queries :: [[integer()]]) -> [integer()]. count_points(Points, Queries) -> do(Queries, Points, []). do([], Points, Ans) -> lists:reverse(Ans); do([[X,Y,R] | Queries], Points, Ans) -> N = lists:foldl(fun([X1,Y1], Acc) -> if R * R >= (X-X1) * (X-X1) + (Y-Y1) * (Y-Y1) -> Acc+1; true -> Acc end end, 0, Points), do(Queries, Points, [N | Ans]).
java 解法, 执行用时: 16 ms, 内存消耗: 42.3 MB, 提交时间: 2023-01-24 21:33:08
class Solution { public int[] countPoints(int[][] points, int[][] queries) { int m = points.length, n = queries.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { int cx = queries[i][0], cy = queries[i][1], cr = queries[i][2]; for (int j = 0; j < m; ++j) { int px = points[j][0], py = points[j][1]; if ((cx - px) * (cx - px) + (cy - py) * (cy - py) <= cr * cr) { ++ans[i]; } } } return ans; } }
python3 解法, 执行用时: 2120 ms, 内存消耗: 15.1 MB, 提交时间: 2023-01-24 21:32:53
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: ans = [0] * len(queries) for i, (cx, cy, cr) in enumerate(queries): for (px, py) in points: if (cx - px) ** 2 + (cy - py) ** 2 <= cr ** 2: ans[i] += 1 return ans
javascript 解法, 执行用时: 100 ms, 内存消耗: 43.6 MB, 提交时间: 2023-01-24 21:32:38
/** * @param {number[][]} points * @param {number[][]} queries * @return {number[]} */ var countPoints = function(points, queries) { const m = points.length, n = queries.length; const ans = new Array(n).fill(0); for (let i = 0; i < n; ++i) { let cx = queries[i][0], cy = queries[i][1], cr = queries[i][2]; for (let j = 0; j < m; ++j) { let px = points[j][0], py = points[j][1]; if ((cx - px) * (cx - px) + (cy - py) * (cy - py) <= cr * cr) { ++ans[i]; } } } return ans; };
golang 解法, 执行用时: 28 ms, 内存消耗: 6.5 MB, 提交时间: 2021-06-29 15:41:23
func countPoints(points [][]int, queries [][]int) []int { var ans []int for _, query := range queries { c := 0 for _, point := range points { if (point[0]-query[0]) * (point[0]-query[0]) + (point[1]-query[1]) * (point[1]-query[1]) <= query[2] * query[2] { c++ } } ans = append(ans, c) } return ans }