Detect Squares
Count how many squares one more point would finish
DSA, Medium, Math & Geometry
Which structure or algorithm, and what does it cost.
Build a class DetectSquares that remembers points on a flat grid. add(x, y) remembers a point. A point may be added again and again, and every copy counts on its own. count(x, y) returns how many ways three remembered points can join the point (x, y) as the corners of a square. The sides must be parallel to the axes, and the area must be above 0. Ways that use different copies are different ways. count does not remember its point. The input is a list of operation strings. The output lists the result of each count, in order.
Examples
Constraints
- Every x and y is a whole number between 0 and 400.
- The list has at most 10,000 operations.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(1) time for add and O(k) time for count, where k is the number of different stored points.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Picture the query point as one corner of a square. Draw a few squares through it and look at what stays fixed.
Hint 2 the approach
One diagonal corner decides the shape of the whole square. Think about what that means for the other corners.
Hint 3 the key step
Try each stored point as the corner opposite the query. What must be true of its x and y distances, and how do copies of the corners combine?
Walkthrough
Full walkthrough brute force to best
Brute force
Keep a plain list of every stored point. For count, try every group of three stored points and test whether they and the query make a square. That is O(m^3) time per count for m stored copies. With 1,000 stored copies it is about 166 million groups for one count.
Best method
Keep a dictionary from point to the number of copies. A square with sides parallel to the axes and one corner at the query is decided by the corner opposite to it. If that corner is (px, py), the other two corners are (px, y) and (x, py). The opposite corner must be as far from the query in x as in y, and that distance must be above 0. Each square has exactly one opposite corner, so no square is counted twice. Copies multiply: if the three corners have a, b and c copies, they make a * b * c squares.
Hand trace
Run on add 2 2, add 6 2, add 6 6, count 2 6. The dictionary holds (2, 2): 1, (6, 2): 1, (6, 6): 1. For count at (2, 6), look at each stored point. (2, 2) has the same x as the query, so the side would be 0: skip. (6, 2) is 4 away in x and 4 away in y, so it is an opposite corner. The other corners are (6, 6) and (2, 2), each stored once, so it adds 1 * 1 * 1 = 1. (6, 6) is 4 away in x but 0 in y: skip. The result is [1].
Code
class DetectSquares:
def __init__(self):
self.copies = {}
def add(self, x, y):
self.copies[(x, y)] = self.copies.get((x, y), 0) + 1
def count(self, x, y):
total = 0
for (px, py), c in self.copies.items():
if abs(px - x) == abs(py - y) and px != x:
total += c * self.copies.get((px, y), 0) * self.copies.get((x, py), 0)
return totalTime and memory
add is O(1). count is O(k) for k different stored points, because each one is checked once with O(1) dictionary lookups. Memory is O(k).
Common mistakes
- Keeping the points in a set. Copies are lost, but each copy must count.
- Skipping the test that the side is above 0. A stored copy of the query point itself would then count as a square.
- Looking only at squares above and to the right of the query. The opposite corner can lie in any of the four directions.
- Checking that the other corners exist, but not that both distances are equal. That counts rectangles.