代码拉取完成,页面将自动刷新
package main
import (
"container/heap"
"sort"
)
type heapArr struct{ sort.IntSlice }
func (h *heapArr) Less(i, j int) bool {
return *&h.IntSlice[i] > *&h.IntSlice[j]
}
func (h *heapArr) Push(x interface{}) {
*&h.IntSlice = append(*&h.IntSlice, x.(int))
}
func (h *heapArr) Pop() interface{} {
old, n := *&h.IntSlice, len(*&h.IntSlice)
x := old[n-1]
old = old[:n-1]
*&h.IntSlice = old
return x
}
func minStoneSum(piles []int, k int) int {
h := heapArr{}
heap.Init(&h)
res := 0
sum := 0
for _, v := range piles {
sum += v
heap.Push(&h, v)
}
for i := 0; i < k; i++ {
top := heap.Pop(&h).(int)
v := top / 2
res += v
top -= v
heap.Push(&h, top)
}
return sum - res
}
func main() {
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。