網頁

2025年12月2日 星期二

Py3 C++ hashmap多項次公式解梯形問題Leetcode 3623 Count Number of Trapezoids I


Py3 CPP多項次公式解梯形問題Leetcode 3623  Count Number of Trapezoids I
數有幾個不同水平梯形,先用hash map數有相同y座標的點,再用多項次公式解題
[Py3解請進]
----
To count the number of different horizontal trapezoids, first use a hash map to count points with the same y-coordinate, then use a quadratic formula to solve the problem.
class Solution:
    def countTrapezoids(self, points: List[List[int]]) -> int:
        freq=Counter(p[1] for p in points)
        Sum, c2=0, 0
        for f in freq.values():
            if f<=1: continue
            c=f*(f-1)//2
            Sum+=c
            c2+=c*c
        return (Sum*Sum-c2)//2%(10**9+7)

沒有留言:

張貼留言

HTML 編輯器