File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int countTrapezoids(vector<vector<int>>& points) {
4+ int n = points.size();
5+ int INF = 1e9 + 7;
6+ unordered_map<float , vector<float>> slopeToIntercept;
7+ unordered_map<int , vector<float>> midToSlope;
8+ int res = 0;
9+
10+ for (int i = 0; i < n; i++) {
11+ int x1 = points[i][0];
12+ int y1 = points[i][1];
13+ for (int j = i + 1; j < n; j++) {
14+ int x2 = points[j][0];
15+ int y2 = points[j][1];
16+ int dx = x1 - x2;
17+ int dy = y1 - y2;
18+
19+ float k , b;
20+ if (x2 == x1) {
21+ k = INF;
22+ b = x1;
23+ }
24+ else {
25+ k = (float)(y2 - y1) / (x2 - x1);
26+ b = (float)(y1 * dx - x1 * dy) / dx;
27+ }
28+
29+ int mid = (x1 + x2) * 10000 + (y1 + y2);
30+ slopeToIntercept[k].push_back(b);
31+ midToSlope[mid].push_back(k);
32+ }
33+ }
34+
35+ for (auto& [_ , sti] : slopeToIntercept) {
36+ if (sti.size() == 1) continue;
37+
38+ map<float , int> count;
39+ for (float b : sti) count[b]++;
40+
41+ int sum = 0;
42+ for (auto& [_ , cnt] : count) {
43+ res += (sum * cnt);
44+ sum += cnt;
45+ }
46+ }
47+
48+ for (auto& [_ , mts] : midToSlope) {
49+ if (mts.size() == 1) continue;
50+
51+ map<float , int> count;
52+ for (float k : mts) count[k]++;
53+
54+ int sum = 0;
55+ for (auto& [_ , cnt] : count) {
56+ res -= (sum * cnt);
57+ sum += cnt;
58+ }
59+ }
60+ return res;
61+ }
62+ };
You can’t perform that action at this time.
0 commit comments