1828. Queries on Number of Points Inside a Circle — Daily Problem
Problem
1828. Queries on Number of Points Inside a Circle
Approach
Today's problem is very simple — I wonder if it's a counterbalance after yesterday's hard one.
The task asks: for each circle in queries, how many points from the points array fall inside it?
Honestly, I was a bit scared at first — thought it would be a graph problem again. But after reading carefully, it's just a straightforward math problem. We can use Euclidean distance to solve it (time complexity O(n²) — I thought there would be a better solution, but at a glance everyone solved it the same way).
The Euclidean distance formula:
See the code for the specific implementation:
Code
class Solution:
def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
# Check if the Euclidean distance from the center is <= r
ans = [0] * len(queries)
flag = 0
for x, y, r in queries:
for i, j in points:
if ((x - i) ** 2 + (y - j) ** 2) ** (1 / 2) <= r:
ans[flag] += 1
flag += 1
return ansAn alternative approach from the community that is harder to understand at first glance:
class Solution:
def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
points = sorted(points)
res = [0 for _ in range(len(queries))]
for i, (u, v, r) in enumerate(queries):
left, right = u - r, u + r
idx1 = bisect_left(points, [left, -inf])
idx2 = bisect_right(points, [right, inf])
for x, y in points[idx1: idx2 + 1]:
if (v - r <= y <= v + r and
(x - u) * (x - u) + (y - v) * (y - v) <= r * r):
res[i] += 1
return res贡献者
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0
1825. Seek out MK average value
LeetCode 1825. 求出 MK 平均值 题解 — 使用三个 multiset 维护滑动窗口中的最小值、中间值和最大值集合,通过平衡插入与删除操作保持 lower 和 upper 各含 k 个元素,并实时维护 middle 的元素和以 O(log n) 计算剔除首尾 k 个后的平均值。适合准备系统设计面试或需要掌握有序集合与滑动窗口技巧的算法学习者。
1828. Statistics the number of a circle mid -point One question daily
LeetCode 1828. 统计一个圆中点的数目 题解 — 使用欧几里得距离公式计算每个点与圆心的距离,判断是否小于等于半径,时间复杂度 O(n^2)。关键技巧是直接遍历所有点与所有圆,无需优化。适合正在刷 LeetCode 每日一题、入门数学类模拟题的求职者和算法初学者。