You are given a 2D integer array points where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane.
Return the number of unique trapezoids that can be formed by choosing any four distinct points from points.
Atrapezoid is a convex quadrilateral with at least one pair of parallel sides. Two lines are parallel if and only if they have the same slope.
Example 1:
Input:points = [[-3,2],[3,0],[2,3],[3,2],[2,-3]]
Output:2
Explanation:
There are two distinct ways to pick four points that form a trapezoid:
The points [-3,2], [2,3], [3,2], [2,-3] form one trapezoid.
The points [2,3], [3,2], [3,0], [2,-3] form another trapezoid.
Example 2:
Input:points = [[0,0],[1,0],[0,1],[2,1]]
Output:1
Explanation:
There is only one trapezoid which can be formed.
Constraints:
4 <= points.length <= 500
–1000 <= xi, yi <= 1000
All points are pairwise distinct.
Solutions
Solution 1: Hash Table + Enumeration
We can combine all points pairwise, calculate the slope and intercept of the line corresponding to each pair of points, record them using a hash table, and calculate the sum of the number of pairs formed by lines with the same slope but different intercepts. Note that for parallelograms, we will count them twice in the above calculation, so we need to subtract them.
The diagonals of a parallelogram share the same midpoint. Therefore, we also combine all points pairwise, calculate the midpoint coordinates and slope of each pair of points, record them using a hash table, and calculate the sum of the number of pairs formed by point pairs with the same slope and the same midpoint coordinates.
Specifically, we use two hash tables \(\textit{cnt1}\) and \(\textit{cnt2}\) to record the following information:
\(\textit{cnt1}\) records the number of occurrences of slope \(k\) and intercept \(b\), with the key being the slope \(k\) and the value being another hash table that records the number of occurrences of intercept \(b\);
\(\textit{cnt2}\) records the number of occurrences of the midpoint coordinates and slope \(k\) of point pairs, with the key being the midpoint coordinates \(p\) of the point pair and the value being another hash table that records the number of occurrences of slope \(k\).
For a point pair \((x_1, y_1)\) and \((x_2, y_2)\), we denote \(dx = x_2 - x_1\) and \(dy = y_2 - y_1\). If \(dx = 0\), it means the two points are on the same vertical line, and we denote the slope \(k = +\infty\) and the intercept \(b = x_1\); otherwise, the slope \(k = \frac{dy}{dx}\) and the intercept \(b = \frac{y_1 \cdot dx - x_1 \cdot dy}{dx}\). The midpoint coordinates \(p\) of the point pair can be expressed as \(p = (x_1 + x_2 + 2000) \cdot 4000 + (y_1 + y_2 + 2000)\), where the offset is added to avoid negative numbers.
Next, we iterate through all point pairs, calculate the corresponding slope \(k\), intercept \(b\), and midpoint coordinates \(p\), and update the hash tables \(\textit{cnt1}\) and \(\textit{cnt2}\).
Then, we iterate through the hash table \(\textit{cnt1}\). For each slope \(k\), we calculate the sum of all pairwise combinations of the occurrence counts of intercept \(b\) and add it to the answer. Finally, we iterate through the hash table \(\textit{cnt2}\). For each midpoint coordinate \(p\), we calculate the sum of all pairwise combinations of the occurrence counts of slope \(k\) and subtract it from the answer.
The time complexity is \(O(n^2)\) and the space complexity is \(O(n^2)\), where \(n\) is the number of points.