39 lines
760 B
Go
39 lines
760 B
Go
package q1046
|
|
|
|
import "container/heap"
|
|
|
|
type IntHeap []int
|
|
|
|
func (h *IntHeap) Len() int { return len(*h) }
|
|
func (h *IntHeap) Less(i int, j int) bool { return (*h)[i] > (*h)[j] }
|
|
func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) }
|
|
func (h *IntHeap) Swap(i int, j int) { (*h)[i], (*h)[j] = (*h)[j], (*h)[i] }
|
|
|
|
func (h *IntHeap) Pop() any {
|
|
ret := (*h)[len(*h)-1]
|
|
*h = (*h)[:len(*h)-1]
|
|
return ret
|
|
}
|
|
|
|
func lastStoneWeight(stones []int) int {
|
|
var st IntHeap = stones
|
|
heap.Init(&st)
|
|
|
|
for st.Len() > 1 {
|
|
a, b := heap.Pop(&st).(int), heap.Pop(&st).(int)
|
|
rem := a - b
|
|
if rem < 0 {
|
|
rem = -rem
|
|
}
|
|
if rem > 0 {
|
|
heap.Push(&st, rem)
|
|
}
|
|
}
|
|
|
|
if st.Len() > 0 {
|
|
return st[0]
|
|
}
|
|
return 0
|
|
}
|
|
|
|
var _ = lastStoneWeight
|