Similar Problems
Similar Problems not available
Detect Squares - Leetcode Solution
LeetCode: Detect Squares Leetcode Solution
Difficulty: Medium
Topics: design hash-table array
Problem statement: You are given a stream of points in the plane where points[i] = [xi, yi]. Each point [xi, yi] represents a point on the 2D plane. We want to know how many squares can be formed by these points.
A square is a quadrilateral with four equal sides and four equal angles (90-degree angles). All four sides and four angles are equal.
Solution:
Approach 1: Brute force This approach involves finding all possible pairs of points and check if they form a square. To do this check, we calculate all possible distances between the points, and check if there exist two pairs of points that have equal distance and intersect at right angles. This approach is inefficient as the time complexity is O(n^4).
Approach 2: Hash Map We can use a hash map to store the frequency of all pairs of points. Two pairs of points can form a square if they have equal distance and intersect at right angles. Therefore, we can calculate the distance and angle between each pair of points, and store them in the hash map using a tuple with distance and angle as key. We can then iterate through the hash map and check if there exist four points that can form a square.
For a pair of points with coordinates (x1, y1) and (x2, y2), the distance is the square root of the sum of the squares of the differences between the coordinates. The angle between the two points is the arctangent of the difference in y-coordinates over the difference in x-coordinates.
The time complexity of this approach is O(n^2 log n) since we have to calculate the distance and angle for each pair of points, and sort the hash maps.
Approach 3: Count occurrences of sides and diagonals We can count the frequency of sides and diagonals with a hash map. For each pair of points, we calculate the distance between them. We add this distance to the hash map with the frequency of this distance. For a square, there should be two pairs of sides with equal distance and a pair of diagonals with equal distance.
We can iterate through the hash map and count the number of pairs of sides and diagonals that form a square. The time complexity of this approach is O(n^2 log n) since we have to calculate the distance for each pair of points, and sort the hash maps.
Approach 4: Count occurrences of diagonal endpoints We can count the frequency of diagonal endpoints with a hash map. For each pair of points, we calculate the distance between them. We add this distance to the hash map with the frequency of this distance and the two endpoints as a tuple. For a square, there should be at least one pair of diagonal endpoints with equal distance.
We can iterate through the hash map and count the number of pairs of diagonal endpoints that form a square. The time complexity of this approach is O(n^2 log n) since we have to calculate the distance for each pair of points, and sort the hash maps.
Approach 5: Count occurrences of midpoints We can count the frequency of midpoints with a hash map. For each pair of points, we calculate the midpoint. We add this midpoint to the hash map with the frequency of this midpoint. For a square, there should be four points with equal distance from the midpoint.
We can iterate through the hash map and count the number of quadruples of midpoints that form a square. The time complexity of this approach is O(n^2) since we only have to calculate the midpoint for each pair of points.
Approach 6: Count occurrences of slopes and intercepts of sides We can count the frequency of slopes and intercepts of sides with a hash map. For each pair of points, we calculate the slope and intercept of the line passing through them. We add this slope-intercept pair to the hash map with the frequency of this pair. For a square, there should be two pairs of sides with the same slope and equal intercept.
We can iterate through the hash map and count the number of pairs of slope-intercept pairs that form a square. The time complexity of this approach is O(n^2) since we only have to calculate the slope and intercept for each pair of points.
Approach 7: Count occurrences of slope and midpoint We can count the frequency of slopes and midpoint with a hash map. For each pair of points, we calculate the slope and midpoint of the line passing through them. We add this slope-midpoint pair to the hash map with the frequency of this pair. For a square, there should be one slope-intercept pair that forms the diagonal of the square and is perpendicular to two other sides.
We can iterate through the hash map and count the number of slope-midpoint pairs that form a square. The time complexity of this approach is O(n^2) since we only have to calculate the slope and midpoint for each pair of points.
Among these approaches, approach 5 and approach 6 are the most efficient with a time complexity of O(n^2) and space complexity of O(n), making them ideal for large datasets.
Detect Squares Solution Code
1