Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/1784727c253a196bc67e5b3a30f7343b to your computer and use it in GitHub Desktop.
Save SuryaPratapK/1784727c253a196bc67e5b3a30f7343b to your computer and use it in GitHub Desktop.
class Solution {
public:
int numberOfPairs(vector<vector<int>>& points) {
int n = points.size();
sort(points.begin(),points.end(),[](const vector<int>& p1,const vector<int>& p2){
return p1[0]==p2[0] ? p1[1]>p2[1] : p1[0]<p2[0];
});
int count = 0;
for(int A=0;A<n-1;++A){
int bottom_right_y = INT_MIN;
for(int B=A+1;B<n;++B){
if(points[B][1]<=points[A][1] and points[B][1]>bottom_right_y){
count++;
bottom_right_y = points[B][1];
}
}
}
return count;
}
};
/*
//JAVA
class Solution {
public int numberOfPairs(int[][] points) {
int n = points.length;
// sort by x asc, and for equal x sort by y desc
Arrays.sort(points, (a, b) -> {
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
return Integer.compare(b[1], a[1]);
});
int count = 0;
for (int A = 0; A < n - 1; ++A) {
int bottomRightY = Integer.MIN_VALUE;
for (int B = A + 1; B < n; ++B) {
if (points[B][1] <= points[A][1] && points[B][1] > bottomRightY) {
count++;
bottomRightY = points[B][1];
}
}
}
return count;
}
}
#Python
from typing import List
class Solution:
def numberOfPairs(self, points: List[List[int]]) -> int:
# sort by x asc, and for equal x sort by y desc
points.sort(key=lambda p: (p[0], -p[1]))
n = len(points)
count = 0
for A in range(n - 1):
bottom_right_y = -10**18 # safe very small value
for B in range(A + 1, n):
if points[B][1] <= points[A][1] and points[B][1] > bottom_right_y:
count += 1
bottom_right_y = points[B][1]
return count
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment