-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathK-Closestpoints.cpp
39 lines (38 loc) · 1.07 KB
/
K-Closestpoints.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
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
vector<vector<int>>po;
int i,sz = points.size(),x,y;
double dist,fron;
unordered_map<double, vector<pair<int,int>> >mp;
priority_queue<double>pq;
for(i=0;i<sz;i++)
{
x = points[i][0];
y = points[i][1];
dist = sqrt(x*x + y*y);
if(pq.size()<K)
{
pq.push(dist);
mp[dist].push_back(make_pair(x,y));
}
else if(pq.size()>=K)
{
if(dist<pq.top())
{
mp.erase(pq.top());
pq.pop();
pq.push(dist);
mp[dist].push_back(make_pair(x,y));
}
}
}
for(auto it=mp.begin();it!=mp.end();it++)
{
vector<pair<int,int>>v = it->second;
for(i=0;i<v.size();i++)
po.push_back({v[i].first,v[i].second});
}
return po;
}
};