-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.go
35 lines (31 loc) · 1.2 KB
/
heap.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
package leetkit
// IntMinHeap is a Min-Heap implementation for integers.
// It allows you to quickly test heap-based solutions.
// However, after testing, you'll need to copy this code and include it in your submission.
type IntMinHeap []int
func (h IntMinHeap) Len() int { return len(h) }
func (h IntMinHeap) Less(i int, j int) bool { return h[i] < h[j] }
func (h IntMinHeap) Swap(i int, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntMinHeap) Push(x any) { *h = append(*h, x.(int)) }
func (h *IntMinHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// IntMaxHeap is a Max-Heap implementation for integers.
// It allows you to quickly test heap-based solutions.
// However, after testing you'll need to copy this code and include it in your submission.
type IntMaxHeap []int
func (h IntMaxHeap) Len() int { return len(h) }
func (h IntMaxHeap) Less(i int, j int) bool { return h[i] > h[j] }
func (h IntMaxHeap) Swap(i int, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntMaxHeap) Push(x any) { *h = append(*h, x.(int)) }
func (h *IntMaxHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}