add new solutions

This commit is contained in:
Yiyang Kang 2026-02-09 10:55:02 +09:00
parent 51975f3386
commit 489fa73880
Signed by: kkyy
SSH key fingerprint: SHA256:lJSbAzC3MvrSORdvIVK6h/3g+rVKJNzM7zq0MgA9WKY
13 changed files with 437 additions and 3 deletions

View file

@ -0,0 +1,39 @@
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