Input: points =[[1,1],[2,2],[3,3]]Output: 0Explanation:

There is no way to choose `A` and `B` so `A`is on the upper left side of `B`.
Input: points =[[6,2],[4,4],[2,6]]Output: 2Explanation:

* The left one is the pair `(points[1], points[0])`, where `points[1]`is on the upper left side of `points[0]` and the rectangle is empty.* The middle one is the pair `(points[2], points[1])`, same as the left one it is a valid pair.* The right one is the pair `(points[2], points[0])`, where `points[2]`is on the upper left side of `points[0]`, but `points[1]`is inside the rectangle so it's not a valid pair.
Input: points =[[3,1],[1,3],[1,1]]Output: 2Explanation:

* The left one is the pair `(points[2], points[0])`, where `points[2]`is on the upper left side of `points[0]` and there are no other points on the line they form. Note that it is a valid state when the two points form a line.* The middle one is the pair `(points[1], points[2])`, it is a valid pair same as the left one.* The right one is the pair `(points[1], points[0])`, it is not a valid pair as `points[2]`is on the border of the rectangle.
We want to count pairs (A, B) such that A is strictly upper-left of B and there are no other points inside or on the rectangle defined by A and B. By sorting points and using a 2D Fenwick Tree, we can efficiently count the number of valid pairs by processing points in order and querying for emptiness.
classSolution {
publicintnumberOfPairs(int[][] points) {
int n = points.length;
Arrays.sort(points, (a, b) -> a[0]== b[0]? a[1]- b[1] : a[0]- b[0]);
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (points[j][0]< points[i][0]&& points[j][1]> points[i][1]) {
boolean ok =true;
for (int k = 0; k < n; ++k) {
if (k == i || k == j) continue;
if (points[j][0]< points[k][0]&& points[k][0]< points[i][0]&& points[i][1]< points[k][1]&& points[k][1]< points[j][1]) {
ok =false;
break;
}
}
if (ok) ans++;
}
}
}
return ans;
}
}
classSolution {
funnumberOfPairs(points: Array<IntArray>): Int {
val n = points.size
points.sortWith(compareBy({ it[0] }, { it[1] }))
var ans = 0for (i in0 until n) {
for (j in0 until i) {
if (points[j][0] < points[i][0] && points[j][1] > points[i][1]) {
var ok = truefor (k in0 until n) {
if (k == i || k == j) continueif (points[j][0] < points[k][0] && points[k][0] < points[i][0] && points[i][1] < points[k][1] && points[k][1] < points[j][1]) {
ok = falsebreak }
}
if (ok) ans++ }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defnumberOfPairs(self, points: list[list[int]]) -> int:
n = len(points)
points.sort()
ans =0for i in range(n):
for j in range(i):
if points[j][0] < points[i][0] and points[j][1] > points[i][1]:
ok =Truefor k in range(n):
if k == i or k == j:
continueif points[j][0] < points[k][0] < points[i][0] and points[i][1] < points[k][1] < points[j][1]:
ok =Falsebreakif ok:
ans +=1return ans
impl Solution {
pubfnnumber_of_pairs(points: Vec<Vec<i32>>) -> i32 {
letmut pts = points;
pts.sort();
let n = pts.len();
letmut ans =0;
for i in0..n {
for j in0..i {
if pts[j][0] < pts[i][0] && pts[j][1] > pts[i][1] {
letmut ok =true;
for k in0..n {
if k == i || k == j { continue; }
if pts[j][0] < pts[k][0] && pts[k][0] < pts[i][0] && pts[i][1] < pts[k][1] && pts[k][1] < pts[j][1] {
ok =false;
break;
}
}
if ok { ans +=1; }
}
}
}
ans
}
}