Skip to content

Instantly share code, notes, and snippets.

@pot-code
Created April 9, 2020 15:57
Show Gist options
  • Save pot-code/e7e31dcc6a3a7b27eba819dc072f876b to your computer and use it in GitHub Desktop.
Save pot-code/e7e31dcc6a3a7b27eba819dc072f876b to your computer and use it in GitHub Desktop.
[data structure] heap and some stuff
type MinHeap []int
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment