[LeetCode] Count Number of Trapezoids I

3623. Count Number of Trapezoids I

You are given a 2D integer array points, where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane.

A horizontal trapezoid is a convex quadrilateral with at least one pair of horizontal sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have the same slope.

Return the number of unique horizontal\ *trapezoids* that can be formed by choosing any four distinct points from points.

Since the answer may be very large, return it modulo 109 + 7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
unordered_map<int,long long> freq;
for(auto& p : points) freq[p[1]]++;
long long res = 0, mod = 1e9 + 7, sum = 0;
for(auto& [_, cnt] : freq) {
long long now = cnt * (cnt - 1) / 2;

res = (res + sum * now % mod) % mod;
sum = (sum + now) % mod;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/07/20/PS/LeetCode/count-number-of-trapezoids-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.