-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathArrayDeque.ts
305 lines (256 loc) · 7.42 KB
/
ArrayDeque.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable generator-star-spacing */
/* eslint-disable no-param-reassign */
// class Deque
// !参考golang的双端队列实现,两个queue头尾拼接实现
// !left尾部 -> left头部 <-> right头部 <- right尾部
import assert from 'assert'
import { Queue_, QueueFast } from './Queue'
/**
* 内部的队列使用头尾指针实现.
* 当入队次数超过`maxEnqueue`时,会使用慢数组.
*/
class ArrayDequeFast<E = number> {
private _left: QueueFast<E>
private _right: QueueFast<E>
/**
* @param maxEnqueue 当入队次数超过该值时,会使用慢数组.
*/
constructor(maxEnqueue: number, iterable: Iterable<E> = []) {
this._left = new QueueFast(maxEnqueue)
this._right = new QueueFast(maxEnqueue)
for (const x of iterable) {
this.push(x)
}
}
push(value: E): this {
this._right.push(value)
return this
}
unshift(value: E): this {
this._left.push(value)
return this
}
pop(): E | undefined {
return this._right.length ? this._right.pop() : this._left.shift()
}
shift(): E | undefined {
return this._left.length ? this._left.pop() : this._right.shift()
}
forEach(callback: (value: E, index: number) => void): void {
const ll = this._left.length
for (let i = 0; i < ll; i++) callback(this._left.at(ll - 1 - i)!, i)
const rl = this._right.length
for (let i = 0; i < rl; i++) callback(this._right.at(i)!, ll + i)
}
/**
* 0<=index<len.
*/
at(index: number): E | undefined {
const ll = this._left.length
if (index < ll) return this._left.at(ll - 1 - index)
return this._right.at(index - ll)
}
front(): E | undefined {
return this._left.length ? this._left.back() : this._right.front()
}
back(): E | undefined {
return this._right.length ? this._right.back() : this._left.front()
}
clear(): void {
this._left.clear()
this._right.clear()
}
clone(): ArrayDequeFast<E> {
const res = new ArrayDequeFast<E>(0)
res._left = this._left.clone()
res._right = this._right.clone()
return res
}
toString(): string {
const sb: string[] = []
this.forEach(x => sb.push(JSON.stringify(x)))
return `ArrayDequeFast{${sb.join(',')}}`
}
empty(): boolean {
return !this._left.length && !this._right.length
}
get length(): number {
return this._left.length + this._right.length
}
}
class ArrayDeque<E = number> {
private _left: Queue_<E>
private _right: Queue_<E>
private _shrinkToFit: boolean
/**
* @param shrinkToFit 出队后,如果剩余元素不足原数组长度的一半,则将数组缩小.默认为`false`.
*/
constructor(iterable: Iterable<E> = [], shrinkToFit = false) {
this._left = new Queue_([], shrinkToFit)
this._right = new Queue_([], shrinkToFit)
for (const x of iterable) {
this.push(x)
}
this._shrinkToFit = shrinkToFit
}
push(value: E): this {
this._right.push(value)
return this
}
unshift(value: E): this {
this._left.push(value)
return this
}
pop(): E | undefined {
return this._right.length ? this._right.pop() : this._left.shift()
}
shift(): E | undefined {
return this._left.length ? this._left.pop() : this._right.shift()
}
forEach(callback: (value: E, index: number) => void): void {
const ll = this._left.length
for (let i = 0; i < ll; i++) callback(this._left.at(ll - 1 - i)!, i)
const rl = this._right.length
for (let i = 0; i < rl; i++) callback(this._right.at(i)!, ll + i)
}
/**
* 0<=index<len.
*/
at(index: number): E | undefined {
const ll = this._left.length
if (index < ll) return this._left.at(ll - 1 - index)
return this._right.at(index - ll)
}
front(): E | undefined {
return this._left.length ? this._left.back() : this._right.front()
}
back(): E | undefined {
return this._right.length ? this._right.back() : this._left.front()
}
clear(): void {
this._left.clear()
this._right.clear()
}
clone(): ArrayDeque<E> {
const res = new ArrayDeque<E>([], this._shrinkToFit)
res._left = this._left.clone()
res._right = this._right.clone()
return res
}
toString(): string {
const sb: string[] = []
this.forEach(x => sb.push(JSON.stringify(x)))
return `ArrayDeque{${sb.join(',')}}`
}
empty(): boolean {
return !this._left.length && !this._right.length
}
get length(): number {
return this._left.length + this._right.length
}
}
export { ArrayDequeFast, ArrayDeque }
if (require.main === module) {
const deque = new ArrayDequeFast<number>(16)
deque.push(1)
deque.unshift(2)
deque.unshift(3)
deque.push(4)
console.log(deque.toString(), deque.at(0), deque.at(1), deque.at(2))
assert.strictEqual(deque.at(0), 3)
assert.strictEqual(deque.at(1), 2)
assert.strictEqual(deque.at(2), 1)
assert.strictEqual(deque.back(), 4)
assert.strictEqual(deque.front(), 3)
// 641. 设计循环双端队列
class MyCircularDeque {
private readonly _capacity: number
private readonly _queue: ArrayDequeFast<number>
constructor(k: number) {
this._capacity = k
this._queue = new ArrayDequeFast(k)
}
insertFront(value: number): boolean {
if (this._queue.length === this._capacity) return false
this._queue.unshift(value)
return true
}
insertLast(value: number): boolean {
if (this._queue.length === this._capacity) return false
this._queue.push(value)
return true
}
deleteFront(): boolean {
if (this._queue.length === 0) return false
this._queue.shift()
return true
}
deleteLast(): boolean {
if (this._queue.length === 0) return false
this._queue.pop()
return true
}
getFront(): number {
return this._queue.front() ?? -1
}
getRear(): number {
return this._queue.back() ?? -1
}
isEmpty(): boolean {
return this._queue.length === 0
}
isFull(): boolean {
return this._queue.length === this._capacity
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* var obj = new MyCircularDeque(k)
* var param_1 = obj.insertFront(value)
* var param_2 = obj.insertLast(value)
* var param_3 = obj.deleteFront()
* var param_4 = obj.deleteLast()
* var param_5 = obj.getFront()
* var param_6 = obj.getRear()
* var param_7 = obj.isEmpty()
* var param_8 = obj.isFull()
*/
const n = 1e7
console.time('FastDeque')
const fdq = new ArrayDequeFast<number>(n)
for (let i = 0; i < n; i++) {
fdq.push(i)
}
for (let i = 0; i < n; i++) {
fdq.shift()
}
for (let i = 0; i < n; i++) {
fdq.unshift(i)
}
for (let i = 0; i < n; i++) {
fdq.pop()
}
for (let i = 0; i < n; i++) {
fdq.at(i)
}
console.timeEnd('FastDeque') // 235.25ms
console.time('Deque')
const dq = new ArrayDeque<number>()
for (let i = 0; i < n; i++) {
dq.push(i)
}
for (let i = 0; i < n; i++) {
dq.shift()
}
for (let i = 0; i < n; i++) {
dq.unshift(i)
}
for (let i = 0; i < n; i++) {
dq.pop()
}
for (let i = 0; i < n; i++) {
dq.at(i)
}
console.timeEnd('Deque') // 620.207ms
}