File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ function numberOfPairs ( points ) {
2
+ // Step 1: Sort points by x ascending, then by y descending
3
+ points . sort ( ( a , b ) => {
4
+ if ( a [ 0 ] === b [ 0 ] ) return b [ 1 ] - a [ 1 ] ;
5
+ return a [ 0 ] - b [ 0 ] ;
6
+ } ) ;
7
+ let pairCount = 0 ;
8
+ const n = points . length ;
9
+
10
+ // Step 2: Iterate through all points as potential "upper-left" points
11
+ for ( let i = 0 ; i < n ; i ++ ) {
12
+ const upperY = points [ i ] [ 1 ] ;
13
+ let lowerYLimit = Number . MIN_SAFE_INTEGER ;
14
+
15
+ // Step 3: Check subsequent points as potential "bottom-right" points
16
+ for ( let j = i + 1 ; j < n ; j ++ ) {
17
+ const currentY = points [ j ] [ 1 ] ;
18
+ if ( currentY <= upperY && currentY > lowerYLimit ) {
19
+ pairCount ++ ;
20
+ lowerYLimit = currentY ;
21
+ if ( currentY === upperY ) break ;
22
+ }
23
+ }
24
+ }
25
+ return pairCount ;
26
+ }
You can’t perform that action at this time.
0 commit comments