add new solutions
This commit is contained in:
parent
51975f3386
commit
489fa73880
13 changed files with 437 additions and 3 deletions
39
solutions/10/q1046/solution.go
Normal file
39
solutions/10/q1046/solution.go
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue