Skip to content

Commit e05fa56

Browse files
authored
Create 3027.js
1 parent c249be5 commit e05fa56

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

3001-3500/3027.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)