Detect Squares
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
arg 1 = ["DetectSquares","add","add","add","count","add","count"]
arg 2 = [[],[[1,1]],[[1,3]],[[3,1]],[[3,3]],[[1,1]],[[3,3]]][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
arg 1 = ["DetectSquares","add","count"]
arg 2 = [[],[[0,0]],[[0,0]]][null,null,0]Explanation
One stored point at the query cannot make a positive-area square.
Example 3
arg 1 = ["DetectSquares","add","add","add","count"]
arg 2 = [[],[[0,0]],[[0,1]],[[1,0]],[[1,1]]][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 contract
DetectSquaresArguments: json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
["DetectSquares","add","add","add","count","add","count"][[],[[1,1]],[[1,3]],[[3,1]],[[3,3]],[[1,1]],[[3,3]]]