Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4508924

Browse files
committed
Update 0973-k-closest-points-to-origin.kt
1 parentb016aeb commit4508924

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
classSolution {
25
funkClosest(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
privatefun Int.squared()=this*this
29+
}
30+
31+
/**
32+
Solution using built in sort function
33+
*/
34+
classSolution {
35+
funkClosest(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 (iin0..k-1) {
39+
list.add(sorted[i])
40+
}
41+
return list.toTypedArray()
42+
}
2643
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp