-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (56 loc) · 1.32 KB
/
main.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package laststoneweight
import (
"container/heap"
"fmt"
"sort"
)
/*
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
type IntSlice []int
func (x IntSlice) Len() int { return len(x) }
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
func (x IntSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
// Sort is a convenience method: x.Sort() calls Sort(x).
func (x IntSlice) Sort() { Sort(x) }
*/
type hp struct {
sort.IntSlice
}
func (h hp) Less(i, j int) bool {
// 大到小排序
return h.IntSlice[i] > h.IntSlice[j]
}
func (h *hp) Push(v interface{}) {
h.IntSlice = append(h.IntSlice, v.(int))
}
func (h *hp) Pop() interface{} {
old := h.IntSlice
v := old[len(old)-1]
h.IntSlice = old[:len(old)-1]
return v
}
func (h *hp) PushInt(v int) {
heap.Push(h, v)
}
func (h *hp) PopInt() int {
return heap.Pop(h).(int)
}
// 時間複雜 O(nlogn), 空間複雜 O(n)
// n 是石頭數量. 每次從隊列中取出元素需要話O(logn) 的時間, 最多共需要需要粉碎 n−1 次石頭
func LastStoneWeight(stones []int) int {
q := &hp{stones}
heap.Init(q)
fmt.Println(q)
for q.Len() > 1 {
fmt.Println(q)
x, y := q.PopInt(), q.PopInt()
fmt.Printf("%d,%d\n", x, y)
if x > y {
q.PushInt(x - y)
}
}
if q.Len() > 0 {
return q.IntSlice[0]
}
return 0
}