Detect Squares - Problem
You are given a stream of points on the X-Y plane. Design an algorithm that:
- Adds new points from the stream into a data structure. Duplicate points are allowed and should be treated as different points.
- Given a query point, counts the number of ways to choose three points from the data structure such that the three points and the query point form an axis-aligned square with positive area.
An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.
Implement the DetectSquares class:
DetectSquares()Initializes the object with an empty data structure.void add(int[] point)Adds a new pointpoint = [x, y]to the data structure.int count(int[] point)Counts the number of ways to form axis-aligned squares with pointpoint = [x, y]as described above.
Input & Output
Example 1 — Basic Square Formation
$
Input:
operations = ["DetectSquares", "add", "add", "add", "count", "count", "add", "count"], points = [[], [3,10], [11,2], [3,2], [11,10], [14,8], [11,2], [11,10]]
›
Output:
[null, null, null, null, 1, 0, null, 1]
💡 Note:
DetectSquares detectSquares = new DetectSquares(); detectSquares.add([3, 10]); detectSquares.add([11, 2]); detectSquares.add([3, 2]); detectSquares.count([11, 10]); // return 1. You can choose: [3, 10], [11, 2], [3, 2] detectSquares.count([14, 8]); // return 0. No valid square. detectSquares.add([11, 2]); // duplicate point detectSquares.count([11, 10]); // return 1 (still same square)
Example 2 — No Valid Squares
$
Input:
operations = ["DetectSquares", "add", "add", "count"], points = [[], [0, 0], [1, 2], [0, 0]]
›
Output:
[null, null, null, 0]
💡 Note:
Points (0,0), (1,2), and query (0,0) cannot form any square with positive area, so count returns 0.
Example 3 — Multiple Squares with Duplicates
$
Input:
operations = ["DetectSquares", "add", "add", "add", "add", "count"], points = [[], [0, 0], [0, 1], [1, 0], [0, 0], [1, 1]]
›
Output:
[null, null, null, null, null, 2]
💡 Note:
Query point (1,1) can form a square with points (0,0), (0,1), (1,0). Since (0,0) appears twice, there are 2 ways to form this square.
Constraints
-
At most 3000 calls in total will be made to
addandcount. -
0 <= point[0], point[1] <= 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Stream
Points are added: (3,10), (11,2), (3,2)
2
Query Processing
Check if query point (11,10) can form squares
3
Square Detection
Found 1 valid square using the 3 stored points
Key Takeaway
🎯 Key Insight: For any two diagonal points of a square, the other two corners are mathematically determined by the side length
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code