Count Number of Trapezoids II Problem
Description
LeetCode Problem 3625.
You are given a 2D integer array points where points[i] = [x_i, y_i] represents the coordinates of the i^th point on the Cartesian plane.
Return the number of unique trapezoids that can be formed by choosing any four distinct points from points.
A trapezoid 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 <= x_i, y_i <= 1000- All points are pairwise distinct.
Solution
Approach: Hash Table + Enumeration
Look at every pair of points and compute the slope and the intercept of the line through them. Two pairs with the same slope but different intercepts lie on two distinct parallel lines, so together they form a trapezoid. Counting those pairs of pairs with a hash table gives every trapezoid, but a parallelogram has two sets of parallel sides and is counted twice.
The diagonals of a parallelogram share the same midpoint. So we also group the pairs of points by midpoint and slope, and subtract the number of pairs of segments that have the same midpoint and different slopes: each of these is a parallelogram.
Two hash tables do the work:
cnt1[k][b]is the number of point pairs whose line has slopekand interceptb;cnt2[p][k]is the number of point pairs with midpointpand slopek.
For points (x1, y1) and (x2, y2) let dx = x2 - x1 and dy = y2 - y1. A vertical line gets the slope 1e9 and the intercept x1. Otherwise the slope is dy / dx and the intercept is (y1 * dx - x1 * dy) / dx. The midpoint is encoded as (x1 + x2 + 2000) * 4001 + (y1 + y2 + 2000): both shifted sums lie in [0, 4000], so the stride has to be 4001.
After filling the tables, each slope group adds the number of pairs of lines with different intercepts, and each midpoint group subtracts the number of pairs of segments with different slopes.
Complexity: O(n^2) time and O(n^2) space, where n is the number of points.
from collections import defaultdict
class Solution:
def countTrapezoids(self, points: List[List[int]]) -> int:
n = len(points)
# cnt1: k -> (b -> count)
cnt1: Dict[float, Dict[float, int]] = defaultdict(lambda: defaultdict(int))
# cnt2: p -> (k -> count)
cnt2: Dict[int, Dict[float, int]] = defaultdict(lambda: defaultdict(int))
for i in range(n):
x1, y1 = points[i]
for j in range(i):
x2, y2 = points[j]
dx, dy = x2 - x1, y2 - y1
if dx == 0:
k = 1e9
b = x1
else:
k = dy / dx
b = (y1 * dx - x1 * dy) / dx
cnt1[k][b] += 1
p = (x1 + x2 + 2000) * 4001 + (y1 + y2 + 2000)
cnt2[p][k] += 1
ans = 0
for e in cnt1.values():
s = 0
for t in e.values():
ans += s * t
s += t
for e in cnt2.values():
s = 0
for t in e.values():
ans -= s * t
s += t
return ans