-
Notifications
You must be signed in to change notification settings - Fork 62
/
min_kth_element.go
44 lines (37 loc) · 924 Bytes
/
min_kth_element.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
package main
import (
"container/heap"
"sort"
)
//最小的k的个数
//排序,取前k个
func getLeastNumbers1(arr []int, k int) []int {
sort.Ints(arr)
return arr[:k]
}
//使用一个堆
func getLeastNumbers2(arr []int, k int) []int {
h := &PriorityQueue{[]int{}}
heap.Init(h)
for _, v := range arr {
heap.Push(h, v)
}
var res []int
for i := 0; i < k; i++ {
res = append(res, heap.Pop(h).(int))
}
return res
}
type PriorityQueue struct {
items []int
}
func (h *PriorityQueue) Len() int { return len(h.items) }
func (h *PriorityQueue) Less(i, j int) bool { return h.items[i] < h.items[j] }
func (h *PriorityQueue) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] }
func (h *PriorityQueue) Push(v interface{}) { h.items = append(h.items, v.(int)) }
func (h *PriorityQueue) Pop() interface{} {
n := len(h.items)
x := h.items[n-1]
h.items = h.items[:n-1]
return x
}