We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc0526c commit b00e38cCopy full SHA for b00e38c
solution/0900-0999/0973.K Closest Points to Origin/Solution3.cpp
@@ -0,0 +1,33 @@
1
+class Solution {
2
+public:
3
+ vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
4
+ int n = points.size();
5
+ int dist[n];
6
+ int r = 0;
7
+ for (int i = 0; i < n; ++i) {
8
+ int x = points[i][0], y = points[i][1];
9
+ dist[i] = x * x + y * y;
10
+ r = max(r, dist[i]);
11
+ }
12
+ int l = 0;
13
+ while (l < r) {
14
+ int mid = (l + r) >> 1;
15
+ int cnt = 0;
16
+ for (int d : dist) {
17
+ cnt += d <= mid;
18
19
+ if (cnt >= k) {
20
+ r = mid;
21
+ } else {
22
+ l = mid + 1;
23
24
25
+ vector<vector<int>> ans;
26
27
+ if (dist[i] <= l) {
28
+ ans.emplace_back(points[i]);
29
30
31
+ return ans;
32
33
+};
0 commit comments