1 Star 0 Fork 0

catyMap/AlgorithmNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
minStoneSum.go 717 Bytes
一键复制 编辑 原始数据 按行查看 历史
catyMap 提交于 2021-08-16 08:30 +08:00 . 新增题库
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() {
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助