-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1381.go
More file actions
52 lines (45 loc) · 864 Bytes
/
Copy pathsolution1381.go
File metadata and controls
52 lines (45 loc) · 864 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
package solution1381
import (
"slices"
)
type CustomStack struct {
stack []int
maxSize int
}
func Constructor(maxSize int) CustomStack {
s := CustomStack{
stack: make([]int, 0, maxSize),
maxSize: maxSize,
}
return s
}
func (s *CustomStack) Push(x int) {
if len(s.stack) == s.maxSize {
return
}
s.stack = append(s.stack, x)
}
func (s *CustomStack) Pop() int {
if len(s.stack) == 0 {
return -1
}
item := s.stack[len(s.stack)-1]
s.stack = slices.Delete(s.stack, len(s.stack)-1, len(s.stack))
return item
}
func (s *CustomStack) Increment(k int, val int) {
n := len(s.stack)
if k < len(s.stack) {
n = k
}
for i := 0; i < n; i++ {
s.stack[i] += val
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* obj := Constructor(maxSize);
* obj.Push(x);
* param_2 := obj.Pop();
* obj.Increment(k,val);
*/