File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
May-LeetCoding-Challenge/30-K-Closest-Points-To-Origin Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ struct T {
4
+ int x, y, d;
5
+ T (){}
6
+ T (int x, int y): x(x), y(y){d = x * x + y * y;}
7
+ bool operator < (const T& tmp) const {
8
+ return d < tmp.d ;
9
+ }
10
+ };
11
+ vector<vector<int >> kClosest (vector<vector<int >>& points, int K) {
12
+ priority_queue<T> q;
13
+ for (int i = 0 ; i < points.size (); i++){
14
+ if (q.size () == K){
15
+ if (q.top ().d > points[i][0 ] * points[i][0 ] + points[i][1 ] * points[i][1 ]) {
16
+ q.pop ();
17
+ q.push (T (points[i][0 ], points[i][1 ]));
18
+ }
19
+ }
20
+ else
21
+ q.push (T (points[i][0 ], points[i][1 ]));
22
+ }
23
+
24
+ vector<vector<int >> ans;
25
+ while (!q.empty ()){
26
+ vector<int > v = {q.top ().x , q.top ().y };
27
+ ans.push_back (v);
28
+ q.pop ();
29
+ }
30
+ return ans;
31
+ }
32
+ };
You can’t perform that action at this time.
0 commit comments