-
Notifications
You must be signed in to change notification settings - Fork 481
/
1381.go
50 lines (40 loc) · 946 Bytes
/
1381.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
type CustomStack struct {
c int
s, inc []int
}
func Constructor(maxSize int) CustomStack {
this := new(CustomStack)
this.c = maxSize
this.s = []int{}
this.inc = []int{}
return *this
}
func (this *CustomStack) Push(x int) {
if len(this.s) < this.c {
this.s = append(this.s, x)
this.inc = append(this.inc, 0)
}
}
func (this *CustomStack) Pop() int {
if len(this.s) == 0 {
return -1
}
if len(this.inc) > 1 {
this.inc[len(this.inc) - 2] += this.inc[len(this.inc) - 1]
}
res := this.s[len(this.s) - 1] + this.inc[len(this.inc) - 1]
this.s = this.s[:len(this.s) - 1]
this.inc = this.inc[:len(this.inc) - 1]
return res
}
func (this *CustomStack) Increment(k int, val int) {
if len(this.inc) > 0 {
this.inc[min(k, len(this.inc)) - 1] += val
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}