forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_stack.go
47 lines (40 loc) · 1.16 KB
/
max_stack.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
package stack
import "errors"
// MaxStack is a stack that can return the maximum of the integers pushed.
type MaxStack struct {
stack1, stack2 []int
}
// ErrEmptyStack occurs when a trying to pop a stack that is empty.
var ErrEmptyStack = errors.New("stack is empty")
// Max solves the problem in O(1) time and O(n) space.
func (maxStack *MaxStack) Max() int {
if len(maxStack.stack2) == 0 {
return -1
}
return maxStack.stack2[len(maxStack.stack2)-1]
}
// Push adds an integer to the stack.
func (maxStack *MaxStack) Push(i int) {
maxStack.stack1 = append(maxStack.stack1, i)
if len(maxStack.stack2) == 0 {
maxStack.stack2 = append(maxStack.stack2, i)
} else {
maxStack.stack2 = append(maxStack.stack2, max(i, maxStack.stack2[len(maxStack.stack2)-1]))
}
}
// Pop returns the last inserted integer.
func (maxStack *MaxStack) Pop() (int, error) {
if len(maxStack.stack1) == 0 {
return -1, ErrEmptyStack
}
tmp := maxStack.stack1[len(maxStack.stack1)-1]
maxStack.stack1 = maxStack.stack1[0 : len(maxStack.stack1)-1]
maxStack.stack2 = maxStack.stack2[0 : len(maxStack.stack2)-1]
return tmp, nil
}
func max(a, b int) int {
if a > b {
return a
}
return b
}