From 9b06416d1999faaf9e08f74f8d80f00a3dba41d2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 2 Dec 2025 07:42:38 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3623 --- .../README.md | 96 ++++++++++++++++++- .../README_EN.md | 96 ++++++++++++++++++- .../Solution.cpp | 17 ++++ .../Solution.go | 15 +++ .../Solution.java | 16 ++++ .../Solution.py | 10 ++ .../Solution.ts | 19 ++++ 7 files changed, 261 insertions(+), 8 deletions(-) create mode 100644 solution/3600-3699/3623.Count Number of Trapezoids I/Solution.cpp create mode 100644 solution/3600-3699/3623.Count Number of Trapezoids I/Solution.go create mode 100644 solution/3600-3699/3623.Count Number of Trapezoids I/Solution.java create mode 100644 solution/3600-3699/3623.Count Number of Trapezoids I/Solution.py create mode 100644 solution/3600-3699/3623.Count Number of Trapezoids I/Solution.ts diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/README.md b/solution/3600-3699/3623.Count Number of Trapezoids I/README.md index c5212363312ed..5ec73499228fd 100644 --- a/solution/3600-3699/3623.Count Number of Trapezoids I/README.md +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/README.md @@ -81,32 +81,120 @@ tags: -### 方法一 +### 方法一:枚举 + +根据题目描述,水平边满足 $y$ 坐标相同,因此我们可以根据 $y$ 坐标将点进行分组,统计每个 $y$ 坐标对应的点的数量。 + +我们用一个哈希表 $\textit{cnt}$ 来存储每个 $y$ 坐标对应的点的数量。对于每个 $y$ 坐标 $y_i$,假设对应的点的数量为 $v$,那么从这些点中选择两点作为水平边的方式有 $\binom{v}{2} = \frac{v(v-1)}{2}$ 种,记为 $t$。 + +我们用一个变量 $s$ 来记录之前所有 $y$ 坐标对应的水平边的数量之和。那么,我们可以将当前 $y$ 坐标对应的水平边的数量 $t$ 与之前所有 $y$ 坐标对应的水平边的数量之和 $s$ 相乘,得到以当前 $y$ 坐标为一对水平边的梯形的数量,并将其累加到答案中。最后,我们将当前 $y$ 坐标对应的水平边的数量 $t$ 累加到 $s$ 中,以便后续计算。 + +注意,由于答案可能非常大,我们需要对 $10^9 + 7$ 取余数。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是点的数量。 #### Python3 ```python - +class Solution: + def countTrapezoids(self, points: List[List[int]]) -> int: + mod = 10**9 + 7 + cnt = Counter(p[1] for p in points) + ans = s = 0 + for v in cnt.values(): + t = v * (v - 1) // 2 + ans = (ans + s * t) % mod + s += t + return ans ``` #### Java ```java - +class Solution { + public int countTrapezoids(int[][] points) { + final int mod = (int) 1e9 + 7; + Map cnt = new HashMap<>(); + for (var p : points) { + cnt.merge(p[1], 1, Integer::sum); + } + long ans = 0, s = 0; + for (int v : cnt.values()) { + long t = 1L * v * (v - 1) / 2; + ans = (ans + s * t) % mod; + s += t; + } + return (int) ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countTrapezoids(vector>& points) { + const int mod = 1e9 + 7; + unordered_map cnt; + for (auto& p : points) { + cnt[p[1]]++; + } + long long ans = 0, s = 0; + for (auto& [_, v] : cnt) { + long long t = 1LL * v * (v - 1) / 2; + ans = (ans + s * t) % mod; + s += t; + } + return (int) ans; + } +}; ``` #### Go ```go +func countTrapezoids(points [][]int) int { + const mod = 1_000_000_007 + cnt := make(map[int]int) + for _, p := range points { + cnt[p[1]]++ + } + + var ans, s int64 + for _, v := range cnt { + t := int64(v) * int64(v-1) / 2 + ans = (ans + s*t) % mod + s += t + } + return int(ans) +} +``` + +#### TypeScript + +```ts +function countTrapezoids(points: number[][]): number { + const mod = 1_000_000_007; + const cnt = new Map(); + + for (const p of points) { + cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1); + } + + let ans = 0; + let s = 0; + for (const v of cnt.values()) { + const t = (v * (v - 1)) / 2; + const mul = BigInt(s) * BigInt(t); + ans = Number((BigInt(ans) + mul) % BigInt(mod)); + s += t; + } + return ans; +} ``` diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/README_EN.md b/solution/3600-3699/3623.Count Number of Trapezoids I/README_EN.md index a63eec5c097cc..7d5bb4852ae3f 100644 --- a/solution/3600-3699/3623.Count Number of Trapezoids I/README_EN.md +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/README_EN.md @@ -79,32 +79,120 @@ tags: -### Solution 1 +### Solution 1: Enumeration + +According to the problem description, horizontal edges have the same $y$ coordinate. Therefore, we can group points by their $y$ coordinates and count the number of points for each $y$ coordinate. + +We use a hash table $\textit{cnt}$ to store the number of points for each $y$ coordinate. For each $y$ coordinate $y_i$, assuming the number of corresponding points is $v$, the number of ways to select two points from these points as a horizontal edge is $\binom{v}{2} = \frac{v(v-1)}{2}$, denoted as $t$. + +We use a variable $s$ to record the sum of the number of horizontal edges for all previous $y$ coordinates. Then, we can multiply the number of horizontal edges $t$ for the current $y$ coordinate by the sum $s$ of the number of horizontal edges for all previous $y$ coordinates to get the number of trapezoids with the current $y$ coordinate as one pair of horizontal edges, and add it to the answer. Finally, we add the number of horizontal edges $t$ for the current $y$ coordinate to $s$ for subsequent calculations. + +Note that since the answer may be very large, we need to take the modulo $10^9 + 7$. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of points. #### Python3 ```python - +class Solution: + def countTrapezoids(self, points: List[List[int]]) -> int: + mod = 10**9 + 7 + cnt = Counter(p[1] for p in points) + ans = s = 0 + for v in cnt.values(): + t = v * (v - 1) // 2 + ans = (ans + s * t) % mod + s += t + return ans ``` #### Java ```java - +class Solution { + public int countTrapezoids(int[][] points) { + final int mod = (int) 1e9 + 7; + Map cnt = new HashMap<>(); + for (var p : points) { + cnt.merge(p[1], 1, Integer::sum); + } + long ans = 0, s = 0; + for (int v : cnt.values()) { + long t = 1L * v * (v - 1) / 2; + ans = (ans + s * t) % mod; + s += t; + } + return (int) ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countTrapezoids(vector>& points) { + const int mod = 1e9 + 7; + unordered_map cnt; + for (auto& p : points) { + cnt[p[1]]++; + } + long long ans = 0, s = 0; + for (auto& [_, v] : cnt) { + long long t = 1LL * v * (v - 1) / 2; + ans = (ans + s * t) % mod; + s += t; + } + return (int) ans; + } +}; ``` #### Go ```go +func countTrapezoids(points [][]int) int { + const mod = 1_000_000_007 + cnt := make(map[int]int) + for _, p := range points { + cnt[p[1]]++ + } + + var ans, s int64 + for _, v := range cnt { + t := int64(v) * int64(v-1) / 2 + ans = (ans + s*t) % mod + s += t + } + return int(ans) +} +``` + +#### TypeScript + +```ts +function countTrapezoids(points: number[][]): number { + const mod = 1_000_000_007; + const cnt = new Map(); + + for (const p of points) { + cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1); + } + + let ans = 0; + let s = 0; + for (const v of cnt.values()) { + const t = (v * (v - 1)) / 2; + const mul = BigInt(s) * BigInt(t); + ans = Number((BigInt(ans) + mul) % BigInt(mod)); + s += t; + } + return ans; +} ``` diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.cpp b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.cpp new file mode 100644 index 0000000000000..a1b4f24d0a14e --- /dev/null +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int countTrapezoids(vector>& points) { + const int mod = 1e9 + 7; + unordered_map cnt; + for (auto& p : points) { + cnt[p[1]]++; + } + long long ans = 0, s = 0; + for (auto& [_, v] : cnt) { + long long t = 1LL * v * (v - 1) / 2; + ans = (ans + s * t) % mod; + s += t; + } + return (int) ans; + } +}; diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.go b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.go new file mode 100644 index 0000000000000..613c9e4a8981b --- /dev/null +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.go @@ -0,0 +1,15 @@ +func countTrapezoids(points [][]int) int { + const mod = 1_000_000_007 + cnt := make(map[int]int) + for _, p := range points { + cnt[p[1]]++ + } + + var ans, s int64 + for _, v := range cnt { + t := int64(v) * int64(v-1) / 2 + ans = (ans + s*t) % mod + s += t + } + return int(ans) +} diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.java b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.java new file mode 100644 index 0000000000000..2e9c12caa9194 --- /dev/null +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int countTrapezoids(int[][] points) { + final int mod = (int) 1e9 + 7; + Map cnt = new HashMap<>(); + for (var p : points) { + cnt.merge(p[1], 1, Integer::sum); + } + long ans = 0, s = 0; + for (int v : cnt.values()) { + long t = 1L * v * (v - 1) / 2; + ans = (ans + s * t) % mod; + s += t; + } + return (int) ans; + } +} diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.py b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.py new file mode 100644 index 0000000000000..9f728713d2d50 --- /dev/null +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def countTrapezoids(self, points: List[List[int]]) -> int: + mod = 10**9 + 7 + cnt = Counter(p[1] for p in points) + ans = s = 0 + for v in cnt.values(): + t = v * (v - 1) // 2 + ans = (ans + s * t) % mod + s += t + return ans diff --git a/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.ts b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.ts new file mode 100644 index 0000000000000..439dd8e464c81 --- /dev/null +++ b/solution/3600-3699/3623.Count Number of Trapezoids I/Solution.ts @@ -0,0 +1,19 @@ +function countTrapezoids(points: number[][]): number { + const mod = 1_000_000_007; + const cnt = new Map(); + + for (const p of points) { + cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1); + } + + let ans = 0; + let s = 0; + for (const v of cnt.values()) { + const t = (v * (v - 1)) / 2; + const mul = BigInt(s) * BigInt(t); + ans = Number((BigInt(ans) + mul) % BigInt(mod)); + s += t; + } + + return ans; +}