Skip to content

Commit 218ca99

Browse files
committed
Create 973-KClosestPointsToOrigin.cpp
1 parent 293ba82 commit 218ca99

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

973-KClosestPointsToOrigin.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
4+
5+
std::multiset<std::pair<long, std::vector<int>>, std::greater<std::pair<long, std::vector<int>>>> ms;
6+
for(long p = 0; p < points.size(); p++){
7+
int x = points[p][0];
8+
int y = points[p][1];
9+
long dist = 1L * x * x + y * y;
10+
ms.insert(std::make_pair(dist, std::vector<int>({x, y})));
11+
if(ms.size() > k){ms.erase(ms.begin());}
12+
}
13+
14+
std::vector<std::vector<int> > v;
15+
for(std::multiset<std::pair<long, std::vector<int> > >::iterator it = ms.begin(); it != ms.end(); it++){
16+
v.push_back(it->second);
17+
}
18+
return v;
19+
}
20+
};

0 commit comments

Comments
 (0)