Skip to content
BASA DSA

Detect Squares

MediumMath & Geometry

Implement DetectSquares with add([x,y]) and count([x,y]). count returns the number of ways to choose three stored point occurrences forming a positive-area axis-aligned square with the query point. Duplicate stored points count separately; the query need not be stored.

Example 1

Input
arg 1 = ["DetectSquares","add","add","add","count","add","count"]
arg 2 = [[],[[1,1]],[[1,3]],[[3,1]],[[3,3]],[[1,1]],[[3,3]]]
Output
[null,null,null,null,1,null,2]

Explanation

Three stored corners complete a square with [3,3]; duplicating [1,1] doubles the choices.

Example 2

Input
arg 1 = ["DetectSquares","add","count"]
arg 2 = [[],[[0,0]],[[0,0]]]
Output
[null,null,0]

Explanation

One stored point at the query cannot make a positive-area square.

Example 3

Input
arg 1 = ["DetectSquares","add","add","add","count"]
arg 2 = [[],[[0,0]],[[0,1]],[[1,0]],[[1,1]]]
Output
[null,null,null,null,1]

Explanation

Three stored corners complete a unit square with unstored query [1,1].

Constraints

  • At most 3000 operations
  • Coordinates are integers 0..1000

Class contractDetectSquaresArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…