-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathput_marbles_in_bags.go
66 lines (52 loc) · 1.24 KB
/
put_marbles_in_bags.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
package main
import "container/heap"
type MinHeap []int
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
type MaxHeap []int
func (h MaxHeap) Len() int { return len(h) }
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MaxHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *MaxHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
func putMarbles(weights []int, k int) int64 {
maxQ := &MaxHeap{}
minQ := &MinHeap{}
heap.Init(maxQ)
heap.Init(minQ)
n := len(weights)
for i := 0; i < n-1; i++ {
heap.Push(maxQ, weights[i]+weights[i+1])
if maxQ.Len() == k {
heap.Pop(maxQ)
}
heap.Push(minQ, weights[i]+weights[i+1])
if minQ.Len() == k {
heap.Pop(minQ)
}
}
var diff int64
for len(*minQ) > 0 {
diff += int64(heap.Pop(minQ).(int)) - int64(heap.Pop(maxQ).(int))
}
return diff
}