@@ -142,3 +142,73 @@ class PriorityQueue {
142
142
return this . last < 1
143
143
}
144
144
}
145
+
146
+ // another
147
+
148
+ /**
149
+ * @param {number } capacity
150
+ */
151
+ const DinnerPlates = function ( capacity ) {
152
+ this . pushIndex = 0
153
+ this . popIndex = 0
154
+ this . capacity = capacity
155
+ this . stacks = [ [ ] ]
156
+ }
157
+
158
+ /**
159
+ * @param {number } val
160
+ * @return {void }
161
+ */
162
+ DinnerPlates . prototype . push = function ( val ) {
163
+ while (
164
+ this . pushIndex < this . stacks . length &&
165
+ this . stacks [ this . pushIndex ] . length === this . capacity
166
+ ) {
167
+ this . pushIndex ++
168
+ }
169
+ if ( this . stacks . length === this . pushIndex ) {
170
+ this . stacks [ this . pushIndex ] = [ val ]
171
+ } else {
172
+ this . stacks [ this . pushIndex ] . push ( val )
173
+ }
174
+ if ( this . popIndex < this . pushIndex ) {
175
+ this . popIndex = this . pushIndex
176
+ }
177
+ }
178
+
179
+ /**
180
+ * @return {number }
181
+ */
182
+ DinnerPlates . prototype . pop = function ( ) {
183
+ while ( this . stacks [ this . popIndex ] . length === 0 ) {
184
+ if ( this . popIndex > 0 ) {
185
+ this . popIndex --
186
+ } else {
187
+ return - 1
188
+ }
189
+ }
190
+ const valueAtIndex = this . stacks [ this . popIndex ] . pop ( )
191
+ if ( this . pushIndex > this . popIndex ) {
192
+ this . pushIndex = this . popIndex
193
+ }
194
+ return valueAtIndex
195
+ }
196
+
197
+ /**
198
+ * @param {number } index
199
+ * @return {number }
200
+ */
201
+ DinnerPlates . prototype . popAtStack = function ( index ) {
202
+ if ( index >= this . stacks . length ) return - 1
203
+ if ( index < this . pushIndex ) this . pushIndex = index
204
+ return this . stacks [ index ] . length > 0 ? this . stacks [ index ] . pop ( ) : - 1
205
+ }
206
+
207
+ /**
208
+ * Your DinnerPlates object will be instantiated and called as such:
209
+ * var obj = new DinnerPlates(capacity)
210
+ * obj.push(val)
211
+ * var param_2 = obj.pop()
212
+ * var param_3 = obj.popAtStack(index)
213
+ */
214
+
0 commit comments