-
Notifications
You must be signed in to change notification settings - Fork 109
/
KClosestPointToOrigin.cpp
58 lines (50 loc) · 1.37 KB
/
KClosestPointToOrigin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
class Point {
public:
int x;
int y;
int distance;
Point(int x, int y, int distance) {
this->x = x;
this->y = y;
this->distance = distance;
}
};
struct comparator {
bool operator() (Point &a, Point &b) {
return (b.distance > a.distance);
}
};
class KClosestPointToOrigin {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
priority_queue<Point, vector<Point>, comparator> maxHeap;
for(int i = 0; i < k; i++) {
int x = points[i][0];
int y = points[i][1];
int distance = x*x + y*y;
Point p = Point(x, y, distance);
maxHeap.push(p);
}
for(int i = k; i < points.size(); i++) {
int x = points[i][0];
int y = points[i][1];
int distance = x*x + y*y;
if(distance < maxHeap.top().distance) {
maxHeap.pop();
maxHeap.push(Point(x, y, distance));
}
}
vector<vector<int>> ans(k,{0,0});
int i = 0;
while(!maxHeap.empty()) {
Point p = maxHeap.top();
maxHeap.pop();
ans[i][0] = p.x;
ans[i][1] = p.y;
i += 1;
}
return ans;
}
};