Problem Description
Design a data structure to add points on the X-Y plane (where duplicate points are allowed) and, given a query point, count the number of ways to form an axis-aligned square (with positive area) using the query point and three other points in the data structure.
Key Insights
- Leverage the fact that an axis-aligned square has vertical and horizontal edges.
- For a query point (x, y), iterate over all points that share the same y-axis. For each of these points (px, y) (with px ≠ x), determine the potential square's side length, d = |px - x|.
- Check for the existence of the two other points (x, y+d) and (px, y+d) (or (x, y-d) and (px, y-d)) that would complete the square.
- Use a hash map (or dictionary) to store each point along with its count since duplicate points are allowed.
Space and Time Complexity
Time Complexity:
• The add operation is O(1).
• The count operation is O(n) in the worst-case scenario, where n is the number of distinct points with the same y-coordinate as the query point.
Space Complexity:
• O(m), where m is the number of added points, since we store counts for each point.
Solution
We implement the DetectSquares class by maintaining a hash map (or dictionary) to store the frequency count for each point using its (x, y) coordinates as the key.
In the add(point) method, we simply increment the count for the given point.
For the count(point) method, we iterate through all points that share the same y-coordinate as the query point. For every such point (px, y) (with px ≠ x), we calculate the potential side length of a square (d = px - x). Then, we check for two possible squares: one above the query point and one below it. The count is increased by the number of times these needed points exist multiplied by the count of the current candidate point.
This method efficiently counts all possible squares that can be formed with the query point.