Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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 <= 200circles[i].length == 31 <= xi, yi <= 1001 <= ri <= min(xi, yi)Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[[2,2,1]]
[[2,2,2],[3,4,1]]
queries-on-number-of-points-inside-a-circle)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2249: Count Lattice Points Inside a Circle
class Solution {
public int countLatticePoints(int[][] circles) {
int mx = 0, my = 0;
for (var c : circles) {
mx = Math.max(mx, c[0] + c[2]);
my = Math.max(my, c[1] + c[2]);
}
int ans = 0;
for (int i = 0; i <= mx; ++i) {
for (int j = 0; j <= my; ++j) {
for (var c : circles) {
int dx = i - c[0], dy = j - c[1];
if (dx * dx + dy * dy <= c[2] * c[2]) {
++ans;
break;
}
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2249: Count Lattice Points Inside a Circle
func countLatticePoints(circles [][]int) (ans int) {
mx, my := 0, 0
for _, c := range circles {
mx = max(mx, c[0]+c[2])
my = max(my, c[1]+c[2])
}
for i := 0; i <= mx; i++ {
for j := 0; j <= my; j++ {
for _, c := range circles {
dx, dy := i-c[0], j-c[1]
if dx*dx+dy*dy <= c[2]*c[2] {
ans++
break
}
}
}
}
return
}
# Accepted solution for LeetCode #2249: Count Lattice Points Inside a Circle
class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
ans = 0
mx = max(x + r for x, _, r in circles)
my = max(y + r for _, y, r in circles)
for i in range(mx + 1):
for j in range(my + 1):
for x, y, r in circles:
dx, dy = i - x, j - y
if dx * dx + dy * dy <= r * r:
ans += 1
break
return ans
// Accepted solution for LeetCode #2249: Count Lattice Points Inside a Circle
/**
* [2249] Count Lattice Points Inside a Circle
*
* Given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and radius ri 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/exa-11.png" style="width: 300px; height: 300px;" />
* 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/exa-22.png" style="width: 300px; height: 300px;" />
* 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)
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-lattice-points-inside-a-circle/
// discuss: https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_lattice_points(circles: Vec<Vec<i32>>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2249_example_1() {
let circles = vec![vec![2, 2, 1]];
let result = 5;
assert_eq!(Solution::count_lattice_points(circles), result);
}
#[test]
#[ignore]
fn test_2249_example_2() {
let circles = vec![vec![2, 2, 2], vec![3, 4, 1]];
let result = 16;
assert_eq!(Solution::count_lattice_points(circles), result);
}
}
// Accepted solution for LeetCode #2249: Count Lattice Points Inside a Circle
function countLatticePoints(circles: number[][]): number {
let mx = 0;
let my = 0;
for (const [x, y, r] of circles) {
mx = Math.max(mx, x + r);
my = Math.max(my, y + r);
}
let ans = 0;
for (let i = 0; i <= mx; ++i) {
for (let j = 0; j <= my; ++j) {
for (const [x, y, r] of circles) {
const dx = i - x;
const dy = j - y;
if (dx * dx + dy * dy <= r * r) {
++ans;
break;
}
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.