Skip to content

Commit 525affe

Browse files
committed
Add solution #3623
1 parent 0fa2893 commit 525affe

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,6 +2784,7 @@
27842784
3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium|
27852785
3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard|
27862786
3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy|
2787+
3623|[Count Number of Trapezoids I](./solutions/3623-count-number-of-trapezoids-i.js)|Medium|
27872788

27882789
## License
27892790

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 3623. Count Number of Trapezoids I
3+
* https://leetcode.com/problems/count-number-of-trapezoids-i/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 2D integer array points, where points[i] = [xi, yi] represents the
7+
* coordinates of the ith point on the Cartesian plane.
8+
*
9+
* A horizontal trapezoid is a convex quadrilateral with at least one pair of horizontal
10+
* sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have
11+
* the same slope.
12+
*
13+
* Return the number of unique horizontal trapezoids that can be formed by choosing any
14+
* four distinct points from points.
15+
*
16+
* Since the answer may be very large, return it modulo 109 + 7.
17+
*/
18+
19+
/**
20+
* @param {number[][]} points
21+
* @return {number}
22+
*/
23+
var countTrapezoids = function(points) {
24+
const MOD = 1e9 + 7;
25+
26+
const map = new Map();
27+
for (const [x, y] of points) {
28+
map.set(y, (map.get(y) || 0) + 1);
29+
}
30+
31+
let result = 0n;
32+
let total = 0n;
33+
for (const count of map.values()) {
34+
const lines = BigInt(count) * BigInt(count - 1) / 2n;
35+
result = (result + total * lines) % BigInt(MOD);
36+
total = (total + lines) % BigInt(MOD);
37+
}
38+
39+
return Number(result);
40+
};

0 commit comments

Comments
 (0)