-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.kt
37 lines (33 loc) · 1018 Bytes
/
Solution.kt
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
class Solution {
private fun dist2(point: IntArray): Int {
return point[0] * point[0] + point[1] * point[1]
}
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
val partition: (Int, Int, Int) -> Int = { l, r, t ->
points[r] = points[t].also { points[t] = points[r] }
var p = l
val d = dist2(points[r])
for (i in l until r) {
if (dist2(points[i]) <= d) {
points[i] = points[p].also { points[p] = points[i] }
p += 1
}
}
points[p] = points[r].also { points[r] = points[p] }
p
}
// [l, r)
var l = 0
var r = points.size
while (r - l > 1) {
val m = l + (r - l) / 2
val t = partition(l, r - 1, m)
if (t >= k) {
r = t
} else {
l = t
}
}
return points.copyOfRange(0, k)
}
}