Skip to the code
cf.
+25 XP

#663 Colliding Sprite Pairs

medium

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 <= 1000
  • 0 <= x, y <= 10^4
  • 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 solves the problem when they all pass.

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