Count Lattice Points Inside a Circle LeetCode Solution

Problem – Count Lattice Points Inside a Circle LeetCode Solution

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:

  • 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)

Count Lattice Points Inside a Circle LeetCode Solution in Python

    def countLatticePoints(self, A: List[List[int]]) -> int:
        res = set()
        for x,y,r in A:
            for i in range(x - r,x + r + 1):
                for j in range(y - r, y + r + 1):
                    if (x - i) ** 2 + (y - j) ** 2 <= r * r:
                        res.add((i,j))
        return len(res)

Count Lattice Points Inside a Circle LeetCode Solution in Java

    public int countLatticePoints(int[][] circles) {
        Set<Integer> res = new HashSet<>();
        for (int[] c : circles)
            for (int i = -c[2]; i <= c[2]; i++)
                for (int j = -c[2]; j <= c[2]; j++)
                    if (i * i + j * j <= c[2] * c[2])
                        res.add((c[0] + i) * 1000 + c[1] + j);
        return res.size();
    }

Count Lattice Points Inside a Circle LeetCode Solution in C++

    int countLatticePoints(vector<vector<int>> circles) {
        unordered_set<int> res;
        for (auto& c : circles)
            for (int i = -c[2]; i <= c[2]; i++)
                for (int j = -c[2]; j <= c[2]; j++)
                    if (i * i + j * j <= c[2] * c[2])
                        res.insert((c[0] + i) * 1000 + c[1] + j);
        return res.size();
    }
Count Lattice Points Inside a Circle LeetCode Solution Review:

In our experience, we suggest you solve this Count Lattice Points Inside a Circle LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Count Lattice Points Inside a Circle LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Count Lattice Points Inside a Circle LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *