-
Notifications
You must be signed in to change notification settings - Fork 0
/
question41.go
60 lines (49 loc) · 1.08 KB
/
question41.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
package chapter07
import "github.com/syhily/code-interviews/common"
type MovingAverage interface {
next(val int) float64
}
type movingAverageSlice struct {
elements []int
start int
size int
sum int
}
func (m *movingAverageSlice) next(val int) float64 {
i := len(m.elements)
if i < m.size {
m.elements = append(m.elements, val)
} else {
m.sum -= m.elements[m.start]
m.elements[m.start] = val
m.start = (m.start + 1) % m.size
}
m.sum += val
return float64(m.sum) / float64(len(m.elements))
}
func newMovingAverageSlice(size int) MovingAverage {
return &movingAverageSlice{
elements: make([]int, 0, size),
start: 0,
size: size,
}
}
type movingAverageQueue struct {
queue common.Queue[int]
sum int
size int
}
func (m *movingAverageQueue) next(val int) float64 {
if m.queue.Size() == m.size {
m.sum -= m.queue.Remove()
}
m.queue.Add(val)
m.sum += val
return float64(m.sum) / float64(m.queue.Size())
}
func newMovingAverageQueue(size int) MovingAverage {
return &movingAverageQueue{
queue: common.NewQueue[int](),
size: size,
}
}