-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01064-last_stone_weight.go
More file actions
53 lines (42 loc) · 870 Bytes
/
01064-last_stone_weight.go
File metadata and controls
53 lines (42 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 1064: Last Stone Weight
// https://leetcode.com/problems/last-stone-weight
package main
import (
"container/heap"
"fmt"
)
type Heap []int
func (h Heap) Len() int { return len(h) }
func (h Heap) Less(i, j int) bool { return h[i] > h[j] }
func (h Heap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *Heap) Push(x any) { *h = append(*h, x.(int)) }
func (h *Heap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// SOLUTION
func lastStoneWeight(stones []int) int {
pq := Heap(stones)
heap.Init(&pq)
for len(pq) > 1 {
y := heap.Pop(&pq).(int)
x := heap.Pop(&pq).(int)
if x != y {
heap.Push(&pq, y-x)
}
}
if len(pq) == 0 {
return 0
}
return pq[0]
}
func main() {
// INPUT
stones := []int{2, 7, 4, 1, 8, 1}
// OUTPUT
result := lastStoneWeight(stones)
fmt.Println(result)
}