Given a 2D integer array circles
where circles[i] = [xi, yi, ri]
represents the center (xi, yi)
and radius ri
of the ith
circle drawn on a grid, return the number of lattice points that are present inside at least one circle.
Note:
- A lattice point is a point with integer coordinates.
- Points that lie on the circumference of a circle are also considered to be inside it.
Example 1:

Input: circles = [[2,2,1]] Output: 5 Explanation: The figure above shows the given circle. The lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green. Other points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle. Hence, the number of lattice points present inside at least one circle is 5.
Example 2:

Input: circles = [[2,2,2],[3,4,1]] Output: 16 Explanation: The figure above shows the given circles. There are exactly 16 lattice points which are present inside at least one circle. Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).
Constraints:
1 <= circles.length <= 200
circles[i].length == 3
1 <= xi, yi <= 100
1 <= ri <= min(xi, yi)
Solution: Brute Force
Time complexity: O(m * m * n) = O(200 * 200 * 200)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Author: Huahua class Solution { public: int countLatticePoints(vector<vector<int>>& circles) { auto isIn = [](int u, int v, int x, int y, int r) { return (u - x) * (u - x) + (v - y) * (v - y) <= r * r; }; int ans = 0; for (int u = 0; u <= 200; ++u) for (int v = 0; v <= 200; ++v) { bool found = false; for (const auto& c : circles) if (isIn(u, v, c[0], c[1], c[2])) { found = true; break; } ans += found; } return ans; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment