|
| 1 | +--- |
| 2 | +title: 1828. Statistics the number of a circle mid -point One question daily |
| 3 | +date: '2024.01.01 0:00' |
| 4 | +tags: |
| 5 | + - - Python |
| 6 | + - - answer |
| 7 | + - - math |
| 8 | +abbrlink: 3277549c |
| 9 | +--- |
| 10 | +# topic: |
| 11 | + |
| 12 | + |
| 13 | +[1828. Statistics the number of a circle mid -point](https://leetcode.cn/problems/queries-on-number-of-points-inside-a-circle/description/) |
| 14 | + |
| 15 | +# Thought: |
| 16 | + |
| 17 | +Today's question is very simple,I wonder if it is against yesterday's question bidding。 |
| 18 | +Ask`queries`There are a few in the circle`points`Points in an array。 |
| 19 | +To be honest, I was scared,I think this question is the question of the picture again。不过仔细读题了后发现只是一道简单的math题, |
| 20 | +We can use European -style distance to solve(Time complexity isOn^2,I thought I had a better solution,At first glance everyone is like this()) |
| 21 | + |
| 22 | +The formula of the European -style distance is as follows: |
| 23 | + |
| 24 | +$\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$ |
| 25 | + |
| 26 | +We look at the code for specific operations: |
| 27 | + |
| 28 | +# Code |
| 29 | + |
| 30 | +```python |
| 31 | +class Solution: |
| 32 | + def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: |
| 33 | + # Seek to solve whether the European -style distance from the center is less than r |
| 34 | + ans = [0] * len(queries) |
| 35 | + flag = 0 |
| 36 | + for x, y, r in queries: |
| 37 | + for i, j in points: |
| 38 | + if ((x - i) ** 2 + (y - j) ** 2) ** (1 / 2) <= r: |
| 39 | + ans[flag] += 1 |
| 40 | + flag += 1 |
| 41 | + return ans |
| 42 | +``` |
| 43 | + |
| 44 | +Take a look at someone who can't understandpythonCode: |
| 45 | + |
| 46 | + |
| 47 | +```python |
| 48 | +class Solution: |
| 49 | + def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: |
| 50 | + points = sorted(points) |
| 51 | + |
| 52 | + res = [0 for _ in range(len(queries))] |
| 53 | + |
| 54 | + for i, (u, v, r) in enumerate(queries): |
| 55 | + left, right = u - r, u + r |
| 56 | + |
| 57 | + idx1 = bisect_left(points, [left, -inf]) |
| 58 | + idx2 = bisect_right(points, [right, inf]) |
| 59 | + |
| 60 | + for x, y in points[idx1: idx2 + 1]: |
| 61 | + if (v - r <= y <= v + r and |
| 62 | + (x - u) * (x - u) + (y - v) * (y - v) <= r * r): |
| 63 | + |
| 64 | + res[i] += 1 |
| 65 | + |
| 66 | + return res |
| 67 | +``` |
0 commit comments