LeetCode 2249: Count Lattice Points Inside a Circle Solution

Master LeetCode problem 2249 (Count Lattice Points Inside a Circle), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2249. Count Lattice Points Inside a Circle

Problem Explanation

Explanation

To solve this problem, we can iterate through each lattice point on the grid and check if it lies inside at least one circle. To determine if a lattice point is inside a circle, we calculate the distance between the lattice point and the center of each circle. If the distance is less than or equal to the radius of the circle, the lattice point is considered inside the circle.

  1. Iterate through each lattice point on the grid.
  2. For each lattice point, iterate through each circle and calculate the distance between the lattice point and the center of the circle.
  3. If the distance is less than or equal to the radius of the circle, increment the count of lattice points inside at least one circle.
  4. Return the total count of lattice points inside circles.

Time complexity: O(n * m) where n is the number of lattice points and m is the number of circles.
Space complexity: O(1)

Solution Code

class Solution {
    public int countLatticePoints(int[][] circles) {
        int count = 0;
        
        for (int x = 1; x <= 100; x++) {
            for (int y = 1; y <= 100; y++) {
                boolean insideCircle = false;
                for (int[] circle : circles) {
                    int cx = circle[0];
                    int cy = circle[1];
                    int r = circle[2];
                    
                    if ((x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r) {
                        insideCircle = true;
                        break;
                    }
                }
                if (insideCircle) {
                    count++;
                }
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2249 (Count Lattice Points Inside a Circle)?

This page provides optimized solutions for LeetCode problem 2249 (Count Lattice Points Inside a Circle) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2249 (Count Lattice Points Inside a Circle)?

The time complexity for LeetCode 2249 (Count Lattice Points Inside a Circle) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2249 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2249 in Java, C++, or Python.

Back to LeetCode Solutions