File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 278427843535|[ Unit Conversion II] ( ./solutions/3535-unit-conversion-ii.js ) |Medium|
278527853539|[ Find Sum of Array Product of Magical Sequences] ( ./solutions/3539-find-sum-of-array-product-of-magical-sequences.js ) |Hard|
278627863541|[ 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
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments