-
Notifications
You must be signed in to change notification settings - Fork 376
/
largest_stack_test.go
89 lines (69 loc) · 1.72 KB
/
largest_stack_test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Problem:
- Implement a stack with a method getMax() that returns the largest element in
the stack in O(1) time.
Approach:
- We use two stack implementation themselves: one holds all the items and the
other holds all the maximum values after each push() and pop().
- That way, we could keep track of your maximum value up to date in constant
time.
Cost:
- O(1) time, O(m) space where m is the number of operations performed on the
stack.
*/
package interviewcake
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestMaxStack(t *testing.T) {
s := newMaxStack()
s.push(2)
common.Equal(t, 2, s.getMax())
s.push(6)
common.Equal(t, 6, s.getMax())
s.push(8)
common.Equal(t, 8, s.getMax())
s.push(4)
common.Equal(t, 8, s.getMax())
common.Equal(t, 4, s.pop())
common.Equal(t, 8, s.getMax())
common.Equal(t, 8, s.pop())
common.Equal(t, 6, s.getMax())
common.Equal(t, 6, s.pop())
common.Equal(t, 2, s.getMax())
common.Equal(t, 2, s.pop())
}
type maxStack struct {
stack *common.Stack
maxStack *common.Stack
}
func newMaxStack() *maxStack {
s := common.NewStack()
ms := common.NewStack()
return &maxStack{
stack: s,
maxStack: ms,
}
}
func (s *maxStack) push(i int) {
// push the item on top of our stack.
s.stack.Push(i)
// only push the item to the maxStack if it greater or equal to the last
// item in maxStack.
if s.maxStack.Size() == 0 || i >= s.maxStack.Top().(int) {
s.maxStack.Push(i)
}
}
func (s *maxStack) pop() int {
// pop the item of our stack.
i := s.stack.Pop().(int)
// if popped item is the largest item, also remove that from maxStack.
if i == s.maxStack.Top().(int) {
s.maxStack.Pop()
}
return i
}
func (s *maxStack) getMax() int {
return s.maxStack.Top().(int)
}