3623. Count Number of Trapezoids I
UnknownView on LeetCode
Time: O(n²)
Space: O(n)
Problem Overview
Count Number of Trapezoids I (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Math pattern in coding interviews. Group points by y; count all pairs of horizontal segments as potential parallel bases.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Group points by y; count all pairs of horizontal segments as potential parallel bases.
3623.cs
C#
// Approach: Group points by y; count all pairs of horizontal segments as potential parallel bases.
// Time: O(n²) Space: O(n)
public class Solution
{
public int CountTrapezoids(int[][] points)
{
const int MOD = 1000000007;
var cnt = new Dictionary<int, int>();
foreach (var point in points)
{
int y = point[1];
if (!cnt.ContainsKey(y))
cnt[y] = 0;
cnt[y]++;
}
long result = 0, total = 0;
foreach (var c in cnt.Values)
{
long curr = ((long)c * (c - 1) / 2) % MOD;
result = (result + (total * curr) % MOD) % MOD;
total = (total + curr) % MOD;
}
return (int)result;
}
}Was this solution helpful?