-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (42 loc) · 783 Bytes
/
main.go
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
package main
import (
"container/heap"
)
func main() {
}
func kClosest(points [][]int, k int) [][]int {
pq := &PQ{}
heap.Init(pq)
for i := 0; i < len(points); i++ {
heap.Push(pq, points[i])
}
var result [][]int
for i := 0; i < k; i++ {
result = append(result, heap.Pop(pq).([]int))
}
return result
}
func getDist(c1 []int) int {
return c1[0]*c1[0] + c1[1]*c1[1]
}
type PQ [][]int
func (pq PQ) Len() int {
return len(pq)
}
func (pq PQ) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
// min-heap
func (pq PQ) Less(i, j int) bool {
return getDist(pq[i]) < getDist(pq[j])
}
func (pq *PQ) Push(x interface{}) {
tmp := x.([]int)
*pq = append(*pq, tmp)
}
func (pq *PQ) Pop() interface{} {
n := len(*pq)
tmp := (*pq)[n-1]
*pq = (*pq)[:n-1]
return tmp
}