This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathdinner-plate-stacks.go
executable file
·110 lines (98 loc) · 2.09 KB
/
dinner-plate-stacks.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package problem1172
// DinnerPlates is ...
type DinnerPlates struct {
cap int
// plate's index of next push
in int
// plate's index of next pop
// when out is -1, plates is empty, unable to pop
out int
plates []*plate
}
// Constructor is ...
func Constructor(capacity int) DinnerPlates {
plates := make([]*plate, 1, 1024)
plates[0] = newPlate(capacity)
return DinnerPlates{
cap: capacity,
in: 0,
out: -1, // not possible to pop at beginning
plates: plates,
}
}
// Push is ...
func (d *DinnerPlates) Push(val int) {
d.plates[d.in].push(val)
// after push into a empty plate at end
// d.out need point to the last nonempty plate
if d.out < d.in {
d.out = d.in
}
// make d.in to be the index of left-most nonfull plate
for d.in < len(d.plates) && d.plates[d.in].isFull() {
d.in++
}
// if no nonfull plate , create a new plate
// JUST NOW, d.out < d.in
if d.in == len(d.plates) {
d.plates = append(d.plates, newPlate(d.cap))
}
}
// Pop is a special condition of PopAtStack
func (d *DinnerPlates) Pop() int {
if d.out == -1 {
return -1
}
return d.PopAtStack(d.out)
}
// PopAtStack is ...
func (d *DinnerPlates) PopAtStack(i int) (res int) {
if len(d.plates) <= i {
return -1
}
p := d.plates[i]
// set value and remove it from the plate
if p.isEmpty() {
return -1
}
res = p.pop()
// make d.in to be the index of left-most nonfull plate
d.in = min(d.in, i)
// PopAtStack could make some empty plate in d.plates
// need jump over these holes
// make sure d.plates[d.out] have val to pop
for d.out >= 0 && d.plates[d.out].isEmpty() {
d.out--
}
return res
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
type plate struct {
cap int
stack []int
}
func newPlate(cap int) *plate {
return &plate{
cap: cap,
stack: make([]int, 0, cap),
}
}
func (p *plate) push(val int) {
p.stack = append(p.stack, val)
}
func (p *plate) pop() (res int) {
n := len(p.stack)
p.stack, res = p.stack[:n-1], p.stack[n-1]
return res
}
func (p *plate) isEmpty() bool {
return len(p.stack) == 0
}
func (p *plate) isFull() bool {
return len(p.stack) == p.cap
}