Skip to the code
+50 XP

#663 Colliding Sprite Pairs

hard

A canvas game checks which sprites collide each frame. Each sprite's hit box is { x, y, width, height }.

Write collidingPairs(sprites) that returns how many pairs of sprites i < j overlap with positive area. Boxes that only touch along an edge or corner don't collide.

Example 1

Input: sprites = [{"x":0,"y":0,"width":10,"height":10},{"x":5,"y":5,"width":10,"height":10},{"x":30,"y":30,"width":5,"height":5}]
Output: 1

Only the first two sprites overlap. The third is far away.

Example 2

Input: sprites = [{"x":0,"y":0,"width":10,"height":10},{"x":10,"y":0,"width":10,"height":10}]
Output: 0

The two boxes only touch along x = 10.

Constraints

  • 0 <= sprites.length <= 10^5
  • 0 <= x, y <= 10^6
  • 1 <= width, height <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).