typeMinHeapstruct{heap[]int}funcNewMinHeap()*MinHeap{return&MinHeap{heap:[]int{}}}func(h*MinHeap)up(kint){ifk==0{return}forpk:=(k-1)/2;k>=0&&h.heap[pk]>h.heap[k];{h.heap[pk],h.heap[k]=h.heap[k],h.heap[pk]k=pkpk=(k-1)/2}}func(h*MinHeap)down(kint){iflen(h.heap)==1{return}last:=(len(h.heap)-2)/2fork<=last{child:=2*k+1// left child
childV:=h.heap[child]right:=child+1ifright<len(h.heap)&&h.heap[right]<childV{child=rightchildV=h.heap[right]}ifh.heap[k]>childV{h.heap[k],h.heap[child]=h.heap[child],h.heap[k]k=child}else{break}}}func(h*MinHeap)add(vint){h.heap=append(h.heap,v)h.up(len(h.heap)-1)}func(h*MinHeap)pop()(int,bool){iflen(h.heap)==0{return0,false}res:=h.heap[0]h.heap[0]=h.heap[len(h.heap)-1]h.heap=h.heap[:len(h.heap)-1]iflen(h.heap)>0{h.down(0)}returnres,true}func(h*MinHeap)heapify(arr[]int){h.heap=arrfori:=(len(h.heap)-2)/2;i>=0;i--{h.down(i)}}