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 b016aeb commit 4508924Copy full SHA for 4508924
kotlin/0973-k-closest-points-to-origin.kt
@@ -1,3 +1,6 @@
1
+/**
2
+Solution using min heap
3
+ */
4
class Solution {
5
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
6
val minHeap = PriorityQueue<IntArray> { a, b -> a[0] - b[0] }
@@ -23,4 +26,18 @@ class Solution {
23
26
}
24
27
25
28
private fun Int.squared() = this * this
29
+}
30
+
31
32
+Solution using built in sort function
33
34
+class Solution {
35
+ fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
36
+ val sorted = points.sortedBy{ it[0]*it[0] + it[1]*it[1]}
37
+ val list = arrayListOf<IntArray>()
38
+ for (i in 0..k-1) {
39
+ list.add(sorted[i])
40
+ }
41
+ return list.toTypedArray()
42
43
0 commit comments