Skip to content

Commit 4508924

Browse files
committed
Update 0973-k-closest-points-to-origin.kt
1 parent b016aeb commit 4508924

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

kotlin/0973-k-closest-points-to-origin.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
Solution using min heap
3+
*/
14
class Solution {
25
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
36
val minHeap = PriorityQueue<IntArray> { a, b -> a[0] - b[0] }
@@ -23,4 +26,18 @@ class Solution {
2326
}
2427

2528
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+
}
2643
}

0 commit comments

Comments
 (0)