FastPrepCount Colliding Object Pairs

Count Colliding Object Pairs

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

Problem statement

Each row of centers contains integer coordinates [x, y] for one object; several objects may share a center.

Two objects collide exactly when both |x1 - x2| <= 2 and |y1 - y2| <= 2. Return the number of unordered colliding index pairs.

Function

countCollidingPairs(centers: int[][]) → long

Examples

Example 1

centers = [[1,1],[2,2],[0,4]]return = 2

The first and second centers collide, and the second and third centers collide.

Example 2

centers = [[0,0],[0,0]]return = 1

Objects sharing a center collide.

Constraints

  • 0 <= centers.length <= 5000
  • Every row contains two integers.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public long countCollidingPairs(int[][] centers) {
    // Write your code here.
}
centers[[1,1],[2,2],[0,4]]
expected2
Checking account…