Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions 3625. Count Number of Trapezoids II
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
int n = points.size();
int INF = 1e9 + 7;
unordered_map<float , vector<float>> slopeToIntercept;
unordered_map<int , vector<float>> midToSlope;
int res = 0;

for (int i = 0; i < n; i++) {
int x1 = points[i][0];
int y1 = points[i][1];
for (int j = i + 1; j < n; j++) {
int x2 = points[j][0];
int y2 = points[j][1];
int dx = x1 - x2;
int dy = y1 - y2;

float k , b;
if (x2 == x1) {
k = INF;
b = x1;
}
else {
k = (float)(y2 - y1) / (x2 - x1);
b = (float)(y1 * dx - x1 * dy) / dx;
}

int mid = (x1 + x2) * 10000 + (y1 + y2);
slopeToIntercept[k].push_back(b);
midToSlope[mid].push_back(k);
}
}

for (auto& [_ , sti] : slopeToIntercept) {
if (sti.size() == 1) continue;

map<float , int> count;
for (float b : sti) count[b]++;

int sum = 0;
for (auto& [_ , cnt] : count) {
res += (sum * cnt);
sum += cnt;
}
}

for (auto& [_ , mts] : midToSlope) {
if (mts.size() == 1) continue;

map<float , int> count;
for (float k : mts) count[k]++;

int sum = 0;
for (auto& [_ , cnt] : count) {
res -= (sum * cnt);
sum += cnt;
}
}
return res;
}
};
Loading