Leetcode 2249: Count Lattice Points Inside a Circle

grid47
grid47
Exploring patterns and algorithms
Mar 27, 2024 6 min read

You are given a 2D integer array circles, where each element circles[i] = [xi, yi, ri] represents a circle with center at (xi, yi) and radius ri. The task is to find the number of lattice points that lie inside at least one of the given circles. A lattice point is defined as a point with integer coordinates, and points lying on the circumference of a circle are also considered inside.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of circles, where each circle is defined by its center coordinates and radius.
Example: circles = [[3, 4, 2], [6, 7, 1]]
Constraints:
• 1 <= circles.length <= 200
• circles[i].length == 3
• 1 <= xi, yi <= 100
• 1 <= ri <= min(xi, yi)
Output: Return an integer representing the number of distinct lattice points inside at least one circle.
Example: Output: 14
Constraints:
Goal: Count the distinct lattice points that are inside or on the circumference of at least one of the given circles.
Steps:
• Iterate through each circle in the input.
• For each circle, iterate through all points in the bounding square defined by the circle's radius.
• Check whether each point lies inside or on the circle using the distance formula.
• Use a data structure like a set to keep track of unique lattice points.
Goal: Ensure that all operations respect the bounds of the input size and circle radius.
Steps:
• Number of circles does not exceed 200.
• All coordinates and radius values are within the specified range.
Assumptions:
• Circle centers and radii are positive integers.
• The grid is large enough to accommodate all specified circles.
Input: circles = [[3, 3, 2]]
Explanation: The circle with center (3, 3) and radius 2 includes lattice points (1, 3), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 2), (4, 3), (4, 4), (5, 3). The total count is 13.

Input: circles = [[2, 2, 1], [4, 4, 1]]
Explanation: The circles include lattice points (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 4), (4, 3), (4, 4), (4, 5), (5, 4). After merging overlapping points, the total count is 10.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus