fork download
  1. // this is a very imp. point thatwe draw a square cell of radius size and the circle surface area
  2. // will only be involved in the 8 nearby cells to its center. So, we explore only the grid
  3. // of 3x3 size for each center/query.
  4.  
  5. // I divide the plane into square cells whose side length equals the radius.
  6. // Any point on or inside the query circle is at most one radius away from the center
  7. // along each axis. Since one radius is one cell width, the point can be at most one column
  8. // and one row away from the center’s cell. So I only need to search the 3 × 3 block:
  9. // the center’s cell and its eight neighbors. Then I check the exact distance for
  10. // each candidate point.
  11.  
  12. /*
  13. Problem: Find All Points Within a Radius
  14.  
  15. Given points [x, y], a fixed radius, and query centers [qx, qy],
  16. return one list of matching original point indices per query.
  17.  
  18. Include points on the circle boundary. Return indices in increasing
  19. order and keep duplicate points as separate indices. Return [] for
  20. a query with no matches.
  21.  
  22. Approach: Build a grid with cell size equal to the positive radius.
  23. For radius 0, use cell size 1. For each query, check its cell and
  24. the eight neighbors, then sort the matching indices.
  25.  
  26. TC: O(n + q + C + Clogc) with average O(1) map lookups => O(n + q + ClogC) when C >= 2
  27. SC: O(n) auxiliary space, O(q + K) output space
  28.  
  29. n = number of points
  30. q = number of queries
  31. C = total candidates checked across all queries
  32. kj = number of matches for query j
  33. K = total matches across all queries
  34. */
  35.  
  36. function pointsWithinRadius(points, radius, queries) {
  37. const cellSize = Math.max(1, radius);
  38. const radiusSquared = radius * radius;
  39. const cells = new Map();
  40.  
  41. // Build the grid once. Each cell stores the original point indices.
  42. for (let i = 0; i < points.length; i++) {
  43. const [x, y] = points[i];
  44. const cx = Math.floor(x / cellSize);
  45. const cy = Math.floor(y / cellSize);
  46. const key = `${cx},${cy}`;
  47.  
  48. if (!cells.has(key)) {
  49. cells.set(key, []);
  50. }
  51.  
  52. cells.get(key).push(i);
  53. }
  54.  
  55. const answer = [];
  56.  
  57. for (const [qx, qy] of queries) {
  58. const cx = Math.floor(qx / cellSize);
  59. const cy = Math.floor(qy / cellSize);
  60. const hits = [];
  61.  
  62. // Check the query cell and its eight neighbors.
  63. for (let gx = cx - 1; gx <= cx + 1; gx++) {
  64. for (let gy = cy - 1; gy <= cy + 1; gy++) {
  65. const bucket = cells.get(`${gx},${gy}`);
  66. if (!bucket) continue;
  67.  
  68. for (const i of bucket) {
  69. const [x, y] = points[i];
  70. const dx = x - qx;
  71. const dy = y - qy;
  72.  
  73. if (dx * dx + dy * dy <= radiusSquared) {
  74. hits.push(i);
  75. }
  76. }
  77. }
  78. }
  79.  
  80. // Cells are visited in grid order, so sort by original index.
  81. hits.sort((a, b) => a - b);
  82. answer.push(hits);
  83. }
  84.  
  85. return answer;
  86. }
  87.  
  88. // Test cases
  89.  
  90. function runTest(name, points, radius, queries, expected) {
  91. const actual = JSON.stringify(pointsWithinRadius(points, radius, queries));
  92. const want = JSON.stringify(expected);
  93.  
  94. console.log(`${actual === want ? "PASS" : "FAIL"} - ${name}`);
  95. console.log(`Expected: ${want}`);
  96. console.log(`Actual: ${actual}\n`);
  97. }
  98.  
  99. // 1. Grid boundary and circle boundary - imp. Dry-run this first.
  100. // Query [2, 1] is in cell (1, 0). Both [0, 1] and [4, 1] are
  101. // exactly 2 units away in neighboring cells. [6, 1] is two cells away.
  102. // The search collects [1, 2, 0], then sorts to [0, 1, 2].
  103. runTest(
  104. "Include boundary points in neighboring cells",
  105. [[4, 1], [0, 1], [3, 0], [6, 1]],
  106. 2,
  107. [[2, 1]],
  108. [[0, 1, 2]]
  109. );
  110.  
  111. // 2. Negative coordinates and a diagonal outside the circle - imp.
  112. // [-2, 0] matches. [2, 2] is in a diagonal neighbor but fails the distance check.
  113. runTest(
  114. "Check negative coordinates and filter a diagonal candidate",
  115. [[-2, 0], [0, 0], [1, 1], [2, 2]],
  116. 2,
  117. [[0, 0]],
  118. [[0, 1, 2]]
  119. );
  120.  
  121. // 3. Radius zero, duplicate points, and no match - imp.
  122. runTest(
  123. "Radius zero and duplicates",
  124. [[2, 2], [2, 2], [3, 2]],
  125. 0,
  126. [[2, 2], [3, 3]],
  127. [[0, 1], []]
  128. );
  129.  
  130. // 4. Multiple queries reuse the same grid.
  131. runTest(
  132. "Reuse the grid across queries",
  133. [[0, 0], [3, 4], [6, 8], [-3, -4], [1, 1]],
  134. 5,
  135. [[0, 0], [6, 8], [10, 10]],
  136. [[0, 1, 3, 4], [1, 2], [2]]
  137. );
Success #stdin #stdout 0.03s 5680KB
stdin
Standard input is empty
stdout
PASS - Include boundary points in neighboring cells
Expected: [[0,1,2]]
Actual:   [[0,1,2]]

PASS - Check negative coordinates and filter a diagonal candidate
Expected: [[0,1,2]]
Actual:   [[0,1,2]]

PASS - Radius zero and duplicates
Expected: [[0,1],[]]
Actual:   [[0,1],[]]

PASS - Reuse the grid across queries
Expected: [[0,1,3,4],[1,2],[2]]
Actual:   [[0,1,3,4],[1,2],[2]]