-
Notifications
You must be signed in to change notification settings - Fork 2
/
K Closest Points to Origin.kt
66 lines (53 loc) · 1.74 KB
/
K Closest Points to Origin.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 코드1: 단순 정렬 */
class Solution {
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
return points.sortedWith(compareBy{
it[0]*it[0] + it[1]*it[1]
}).subList(0, k).toTypedArray()
}
}
/* 코드2: maxHeap 사용 */
class Solution {
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
val maxPQ = PriorityQueue<IntArray>(k, compareByDescending{ it: IntArray ->
it[0]*it[0] + it[1]*it[1]
})
for (i in points.indices) {
maxPQ.add(points[i])
if (maxPQ.size > k) {
maxPQ.poll()
}
}
return maxPQ.toTypedArray()
}
}
/* 코드3: 퀵소트 + 이진 탐색 */
class Solution {
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
var start = 0
var end = points.lastIndex
while(start < end) {
val mid = quickSortAndGetPivot(points, start, end)
if (mid == k) break
else if (mid < k) start = mid + 1
else end = mid - 1
}
return Arrays.copyOf(points, k)
}
private fun quickSortAndGetPivot(points: Array<IntArray>, s: Int, e: Int): Int {
var start = s
var end = e
val pivot = points[start]
while(start < end) {
while (start < end && compare(pivot, points[end]) <= 0) end--
points[start] = points[end]
while (start < end && compare(pivot, points[start]) >= 0) start++
points[end] = points[start]
}
points[start] = pivot
return start
}
private fun compare(p1: IntArray, p2: IntArray): Int {
return p1[0]*p1[0] + p1[1]*p1[1] - p2[0]*p2[0] - p2[1]*p2[1]
}
}