Count Lattice Points Inside a Circle - Complete Solution Guide
Count Lattice Points Inside a Circle is LeetCode problem 2249, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a 2D integer array circles where circles[i] = [x i , y i , r i ] represents the center (x i , y i ) and radius r i of the i th 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 po
Detailed Explanation
The problem asks us to count the number of unique integer coordinate points (lattice points) that lie inside or on the boundary of at least one circle from a given set of circles. Each circle is defined by its center (x, y) and radius r. The input is a 2D array 'circles' where each row represents a circle [x, y, r]. The output is the total count of distinct lattice points covered by one or more circles.
Solution Approach
The solution iterates through each circle and, for each circle, iterates through all possible lattice points within a square bounding box centered at the circle's center and with side length 2*r. For each point within the bounding box, it checks if the point is inside the circle using the circle equation. If it is, the point is added to a set. Finally, the size of the set is returned, representing the total number of unique lattice points within the circles.
Step-by-Step Algorithm
- Step 1: Initialize an empty set called 'points' to store the unique lattice points.
- Step 2: Iterate through each circle in the input array 'circles'.
- Step 3: For each circle with center (cx, cy) and radius r, iterate through all possible x coordinates from cx - r to cx + r (inclusive).
- Step 4: For each x coordinate, iterate through all possible y coordinates from cy - r to cy + r (inclusive).
- Step 5: For each point (x, y), check if it lies inside or on the circle using the distance formula (or circle equation): (x - cx)^2 + (y - cy)^2 <= r^2
- Step 6: If the point (x, y) lies inside or on the circle, add it to the 'points' set.
- Step 7: After iterating through all circles and points, return the size of the 'points' set.
Key Insights
- Insight 1: Since the constraints on x, y, and r are relatively small (1 <= x, y <= 100, 1 <= r <= min(x, y)), a brute-force approach of iterating through all possible lattice points within a bounding box defined by the circles' coordinates is feasible.
- Insight 2: Using a set data structure is crucial to efficiently track and count only the *unique* lattice points.
- Insight 3: The distance formula (or the circle equation) is fundamental to determining if a point lies inside a circle: (x - cx)^2 + (y - cy)^2 <= r^2
Complexity Analysis
Time Complexity: O(n*r^2)
Space Complexity: O(n*r^2)
Topics
This problem involves: Array, Hash Table, Math, Geometry, Enumeration.
Companies
Asked at: Rubrik.