DDSA
Advertisement

3623. Count Number of Trapezoids I

Time: O(n²)
Space: O(n)

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;
    }
}
Advertisement
Was this solution helpful?