-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0973_k_closest_points_to_origin.go
More file actions
64 lines (50 loc) · 1.39 KB
/
s0973_k_closest_points_to_origin.go
File metadata and controls
64 lines (50 loc) · 1.39 KB
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
/*
https://leetcode.com/problems/k-closest-points-to-origin/
Given an array of points where points[i] = [xi, yi] represents a point on the
X-Y plane and an integer k,
return the k closest points to the origin (0, 0).
The distance between two points on the X-Y plane is the Euclidean distance
(i.e., √(x1 - x2)2 + (y1 - y2)2).
You may return the answer in any order. The answer is guaranteed to be unique
(except for the order that it is in).
*/
//nolint:revive // it's ok
package solutions
import (
"container/heap"
"math"
)
type Point struct {
X, Y int
Dist float64
}
type pointHeap []*Point
func (h pointHeap) Len() int { return len(h) }
func (h pointHeap) Less(i, j int) bool { return h[i].Dist > h[j].Dist }
func (h pointHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *pointHeap) Push(x interface{}) { *h = append(*h, x.(*Point)) }
func (h *pointHeap) Pop() interface{} {
x := (*h)[h.Len()-1]
*h = (*h)[:h.Len()-1]
return x
}
func getDist(x, y int) float64 {
fx, fy := float64(x), float64(y)
return math.Sqrt(fx*fx + fy*fy)
}
func kClosest(points [][]int, k int) [][]int {
h := &pointHeap{}
for _, p := range points {
x, y := p[0], p[1]
heap.Push(h, &Point{X: x, Y: y, Dist: getDist(x, y)})
if h.Len() > k {
heap.Pop(h)
}
}
var res [][]int
for h.Len() > 0 {
p := heap.Pop(h).(*Point)
res = append(res, []int{p.X, p.Y})
}
return res
}