|
1 | 1 | package Solution |
2 | 2 |
|
3 | | -func Solution(x bool) bool { |
| 3 | +import "container/heap" |
| 4 | + |
| 5 | +type heapItem1172 struct { |
| 6 | + stack []int |
| 7 | + |
| 8 | + index, sourceIndex int |
| 9 | +} |
| 10 | + |
| 11 | +type leftNotFullHeap struct { |
| 12 | + data []*heapItem1172 |
| 13 | + capacity int |
| 14 | +} |
| 15 | + |
| 16 | +func (h *leftNotFullHeap) Len() int { |
| 17 | + return len(h.data) |
| 18 | +} |
| 19 | + |
| 20 | +func (h *leftNotFullHeap) Less(i, j int) bool { |
| 21 | + a, b := h.data[i], h.data[j] |
| 22 | + la, lb := len(a.stack), len(b.stack) |
| 23 | + if la == h.capacity { |
| 24 | + return false |
| 25 | + } |
| 26 | + if lb == h.capacity { |
| 27 | + return true |
| 28 | + } |
| 29 | + return a.sourceIndex < b.sourceIndex |
| 30 | +} |
| 31 | + |
| 32 | +func (h *leftNotFullHeap) Swap(i, j int) { |
| 33 | + h.data[i], h.data[j] = h.data[j], h.data[i] |
| 34 | + h.data[i].index = i |
| 35 | + h.data[j].index = j |
| 36 | +} |
| 37 | + |
| 38 | +func (h *leftNotFullHeap) Push(x any) { |
| 39 | + item := x.(*heapItem1172) |
| 40 | + l := len(h.data) |
| 41 | + item.index = l |
| 42 | + h.data = append(h.data, item) |
| 43 | +} |
| 44 | + |
| 45 | +func (h *leftNotFullHeap) Pop() any { |
| 46 | + old := h.data |
| 47 | + l := len(old) |
| 48 | + x := old[l-1] |
| 49 | + h.data = old[:l-1] |
4 | 50 | return x |
5 | 51 | } |
| 52 | + |
| 53 | +type rightHeapItem1172 struct { |
| 54 | + sourceIndex, index int |
| 55 | +} |
| 56 | +type rightNotEmptyHeap struct { |
| 57 | + data []*rightHeapItem1172 |
| 58 | + list *[]*heapItem1172 |
| 59 | +} |
| 60 | + |
| 61 | +func (h *rightNotEmptyHeap) Len() int { |
| 62 | + return len(h.data) |
| 63 | +} |
| 64 | + |
| 65 | +func (h *rightNotEmptyHeap) Less(i, j int) bool { |
| 66 | + a, b := h.data[i], h.data[j] |
| 67 | + ia, ib := (*h.list)[a.sourceIndex], (*h.list)[b.sourceIndex] |
| 68 | + li, lj := len(ia.stack), len(ib.stack) |
| 69 | + if li == 0 && lj == 0 || (li != 0 && lj != 0) { |
| 70 | + return a.sourceIndex > b.sourceIndex |
| 71 | + } |
| 72 | + return li != 0 |
| 73 | +} |
| 74 | + |
| 75 | +func (h *rightNotEmptyHeap) Swap(i, j int) { |
| 76 | + h.data[i], h.data[j] = h.data[j], h.data[i] |
| 77 | + h.data[i].index = i |
| 78 | + h.data[j].index = j |
| 79 | +} |
| 80 | + |
| 81 | +func (h *rightNotEmptyHeap) Push(x any) { |
| 82 | + item := x.(*rightHeapItem1172) |
| 83 | + item.index = len(h.data) |
| 84 | + h.data = append(h.data, item) |
| 85 | +} |
| 86 | + |
| 87 | +func (h *rightNotEmptyHeap) Pop() any { |
| 88 | + l := len(h.data) |
| 89 | + x := h.data[l-1] |
| 90 | + h.data = h.data[:l-1] |
| 91 | + return x |
| 92 | +} |
| 93 | + |
| 94 | +type DinnerPlates struct { |
| 95 | + leftNotFull *leftNotFullHeap |
| 96 | + rightNotEmpty *rightNotEmptyHeap |
| 97 | + capacity int |
| 98 | + leftIndies *[]*heapItem1172 |
| 99 | + |
| 100 | + rightIndies map[int]*rightHeapItem1172 |
| 101 | +} |
| 102 | + |
| 103 | +func Constructor(capacity int) DinnerPlates { |
| 104 | + indies := make([]*heapItem1172, 0) |
| 105 | + return DinnerPlates{ |
| 106 | + leftNotFull: &leftNotFullHeap{ |
| 107 | + data: make([]*heapItem1172, 0), |
| 108 | + capacity: capacity, |
| 109 | + }, |
| 110 | + rightNotEmpty: &rightNotEmptyHeap{ |
| 111 | + data: make([]*rightHeapItem1172, 0), |
| 112 | + list: &indies, |
| 113 | + }, |
| 114 | + capacity: capacity, |
| 115 | + leftIndies: &indies, |
| 116 | + rightIndies: make(map[int]*rightHeapItem1172), |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func (this *DinnerPlates) Push(val int) { |
| 121 | + if len(this.leftNotFull.data) == 0 || len(this.leftNotFull.data[0].stack) == this.capacity { |
| 122 | + sourceIndex := len(this.leftNotFull.data) |
| 123 | + leftItem := &heapItem1172{ |
| 124 | + stack: []int{val}, |
| 125 | + sourceIndex: sourceIndex, |
| 126 | + } |
| 127 | + rightItem := &rightHeapItem1172{ |
| 128 | + sourceIndex: sourceIndex, |
| 129 | + } |
| 130 | + this.rightIndies[sourceIndex] = rightItem |
| 131 | + |
| 132 | + *this.leftIndies = append(*this.leftIndies, leftItem) |
| 133 | + heap.Push(this.leftNotFull, leftItem) |
| 134 | + heap.Push(this.rightNotEmpty, rightItem) |
| 135 | + return |
| 136 | + } |
| 137 | + zero := this.leftNotFull.data[0] |
| 138 | + zero.stack = append(zero.stack, val) |
| 139 | + heap.Fix(this.leftNotFull, 0) |
| 140 | + heap.Fix(this.rightNotEmpty, this.rightIndies[zero.sourceIndex].index) |
| 141 | +} |
| 142 | + |
| 143 | +func (this *DinnerPlates) Pop() int { |
| 144 | + top := this.rightNotEmpty.data[0] |
| 145 | + return this.PopAtStack(top.sourceIndex) |
| 146 | +} |
| 147 | + |
| 148 | +func (this *DinnerPlates) PopAtStack(index int) int { |
| 149 | + if index < 0 || index >= len(*this.leftIndies) { |
| 150 | + return -1 |
| 151 | + } |
| 152 | + v := (*this.leftIndies)[index] |
| 153 | + l := len(v.stack) |
| 154 | + if l == 0 { |
| 155 | + return -1 |
| 156 | + } |
| 157 | + x := v.stack[l-1] |
| 158 | + v.stack = v.stack[:l-1] |
| 159 | + heap.Fix(this.leftNotFull, v.index) |
| 160 | + heap.Fix(this.rightNotEmpty, this.rightIndies[v.sourceIndex].index) |
| 161 | + return x |
| 162 | +} |
| 163 | + |
| 164 | +type opt struct { |
| 165 | + name string |
| 166 | + i int |
| 167 | +} |
| 168 | + |
| 169 | +func Solution(capacity int, opts []opt) []int { |
| 170 | + c := Constructor(capacity) |
| 171 | + var ret []int |
| 172 | + for _, op := range opts { |
| 173 | + if op.name == "push" { |
| 174 | + c.Push(op.i) |
| 175 | + continue |
| 176 | + } |
| 177 | + if op.name == "popAtStack" { |
| 178 | + ret = append(ret, c.PopAtStack(op.i)) |
| 179 | + continue |
| 180 | + } |
| 181 | + if op.name == "pop" { |
| 182 | + ret = append(ret, c.Pop()) |
| 183 | + } |
| 184 | + } |
| 185 | + return ret |
| 186 | +} |
0 commit comments