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 b00e38c commit cdbc09fCopy full SHA for cdbc09f
solution/0900-0999/0973.K Closest Points to Origin/Solution3.go
@@ -0,0 +1,29 @@
1
+func kClosest(points [][]int, k int) (ans [][]int) {
2
+ n := len(points)
3
+ dist := make([]int, n)
4
+ l, r := 0, 0
5
+ for i, p := range points {
6
+ dist[i] = p[0]*p[0] + p[1]*p[1]
7
+ r = max(r, dist[i])
8
+ }
9
+ for l < r {
10
+ mid := (l + r) >> 1
11
+ cnt := 0
12
+ for _, d := range dist {
13
+ if d <= mid {
14
+ cnt++
15
16
17
+ if cnt >= k {
18
+ r = mid
19
+ } else {
20
+ l = mid + 1
21
22
23
24
+ if dist[i] <= l {
25
+ ans = append(ans, p)
26
27
28
+ return
29
+}
0 commit comments