#3623
Count Number of Trapezoids I
MediumArrayHash TableMathGeometry
Approaches
💡
Intuition
Time Space
We can check all combinations of four points to see if they form a trapezoid. This is straightforward but inefficient for larger datasets.
⚙️
Algorithm
3 steps- 1Step 1: Iterate through all combinations of four distinct points.
- 2Step 2: Check if two pairs of points have the same y-coordinate (horizontal sides).
- 3Step 3: Count valid trapezoids.
solution.py8 lines
1from itertools import combinations
2
3def count_trapezoids(points):
4 count = 0
5 for p1, p2, p3, p4 in combinations(points, 4):
6 if (p1[1] == p2[1] and p3[1] == p4[1]) or (p1[1] == p3[1] and p2[1] == p4[1]) or (p1[1] == p4[1] and p2[1] == p3[1]):
7 count += 1
8 return count % (10**9 + 7)Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.