data-structure-typed 1.54.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/benchmark/report.html +26 -2
- package/benchmark/report.json +292 -42
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
- package/dist/cjs/data-structures/base/linear-base.js +553 -0
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +67 -5
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +1 -7
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +1 -7
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +16 -16
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
- package/dist/cjs/data-structures/heap/heap.js +0 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
- package/dist/cjs/data-structures/queue/deque.js +269 -169
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +131 -40
- package/dist/cjs/data-structures/queue/queue.js +181 -50
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +124 -11
- package/dist/cjs/data-structures/stack/stack.js +121 -10
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
- package/dist/cjs/data-structures/trie/trie.js +3 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
- package/dist/esm/data-structures/base/linear-base.js +549 -0
- package/dist/esm/data-structures/base/linear-base.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +50 -36
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +62 -0
- package/dist/esm/data-structures/binary-tree/binary-tree.js +67 -5
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/esm/data-structures/binary-tree/bst.js +14 -12
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +1 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +1 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +210 -39
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +16 -16
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/esm/data-structures/hash/hash-map.js +46 -0
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +3 -11
- package/dist/esm/data-structures/heap/heap.js +0 -10
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +283 -170
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +130 -91
- package/dist/esm/data-structures/queue/deque.js +269 -169
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +131 -40
- package/dist/esm/data-structures/queue/queue.js +182 -51
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +124 -11
- package/dist/esm/data-structures/stack/stack.js +121 -10
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +4 -3
- package/dist/esm/data-structures/trie/trie.js +3 -0
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/types/data-structures/base/base.d.ts +9 -4
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
- package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
- package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
- package/dist/individuals/binary-tree/bst.mjs +3858 -0
- package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
- package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
- package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
- package/dist/individuals/graph/directed-graph.mjs +2910 -0
- package/dist/individuals/graph/undirected-graph.mjs +2745 -0
- package/dist/individuals/hash/hash-map.mjs +1040 -0
- package/dist/individuals/heap/heap.mjs +909 -0
- package/dist/individuals/heap/max-heap.mjs +671 -0
- package/dist/individuals/heap/min-heap.mjs +659 -0
- package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
- package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
- package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
- package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
- package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
- package/dist/individuals/queue/deque.mjs +1262 -0
- package/dist/individuals/queue/queue.mjs +1865 -0
- package/dist/individuals/stack/stack.mjs +415 -0
- package/dist/individuals/trie/trie.mjs +687 -0
- package/dist/umd/data-structure-typed.js +1260 -649
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +168 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +15 -14
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +165 -7
- package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +183 -12
- package/test/unit/data-structures/queue/deque.test.ts +241 -60
- package/test/unit/data-structures/queue/queue.test.ts +332 -19
- package/test/unit/data-structures/stack/stack.test.ts +165 -0
- package/test/unit/unrestricted-interconversion.test.ts +1 -1
|
@@ -11,21 +11,21 @@ describe('Queue', () => {
|
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
it('new Queue() should create an empty queue', () => {
|
|
14
|
-
expect(queue.
|
|
14
|
+
expect(queue.length).toBe(0);
|
|
15
15
|
expect(queue.isEmpty()).toBeTruthy();
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
it('push should add elements to the queue', () => {
|
|
19
19
|
queue.push(1);
|
|
20
20
|
queue.push(2);
|
|
21
|
-
expect(queue.
|
|
21
|
+
expect(queue.length).toBe(2);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('shift should remove the first element', () => {
|
|
25
25
|
queue.push(1);
|
|
26
26
|
queue.push(2);
|
|
27
27
|
expect(queue.shift()).toBe(1);
|
|
28
|
-
expect(queue.
|
|
28
|
+
expect(queue.length).toBe(1);
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
it('shift should return undefined if queue is empty', () => {
|
|
@@ -36,17 +36,17 @@ describe('Queue', () => {
|
|
|
36
36
|
queue.push(1);
|
|
37
37
|
queue.push(2);
|
|
38
38
|
expect(queue.first).toBe(1);
|
|
39
|
-
expect(queue.
|
|
39
|
+
expect(queue.length).toBe(2);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
42
|
it('first should return undefined if queue is empty', () => {
|
|
43
43
|
expect(queue.first).toBeUndefined();
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
it('
|
|
46
|
+
it('length should return the number of elements', () => {
|
|
47
47
|
queue.push(1);
|
|
48
48
|
queue.push(2);
|
|
49
|
-
expect(queue.
|
|
49
|
+
expect(queue.length).toBe(2);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
it('isEmpty should return true if the queue is empty', () => {
|
|
@@ -68,7 +68,7 @@ describe('Queue', () => {
|
|
|
68
68
|
queue.push(1);
|
|
69
69
|
queue.push(2);
|
|
70
70
|
queue.clear();
|
|
71
|
-
expect(queue.
|
|
71
|
+
expect(queue.length).toBe(0);
|
|
72
72
|
});
|
|
73
73
|
|
|
74
74
|
it('forEach should iterate over all elements', () => {
|
|
@@ -90,6 +90,16 @@ describe('Queue', () => {
|
|
|
90
90
|
expect(queue.isEmpty()).toBeTruthy();
|
|
91
91
|
});
|
|
92
92
|
|
|
93
|
+
it('push with maxLen', () => {
|
|
94
|
+
const queue = new Queue<number>([], { maxLen: 10 });
|
|
95
|
+
for (let i = 0; i < 1000; i++) {
|
|
96
|
+
queue.push(i);
|
|
97
|
+
}
|
|
98
|
+
expect(queue.maxLen).toBe(10);
|
|
99
|
+
expect(queue.length).toBe(10);
|
|
100
|
+
expect(queue.first).toBe(990);
|
|
101
|
+
});
|
|
102
|
+
|
|
93
103
|
it('compact method should work well', () => {
|
|
94
104
|
for (let i = 0; i < 1000; i++) queue.push(i);
|
|
95
105
|
|
|
@@ -119,9 +129,9 @@ describe('Queue', () => {
|
|
|
119
129
|
toElementFn: rawElement => rawElement.id
|
|
120
130
|
});
|
|
121
131
|
|
|
122
|
-
expect(queue.
|
|
132
|
+
expect(queue.length).toBe(5);
|
|
123
133
|
queue.shift();
|
|
124
|
-
expect(queue.
|
|
134
|
+
expect(queue.length).toBe(4);
|
|
125
135
|
expect(queue.at(1)).toBe('3');
|
|
126
136
|
});
|
|
127
137
|
|
|
@@ -193,11 +203,11 @@ describe('Queue', () => {
|
|
|
193
203
|
queue.push(9);
|
|
194
204
|
queue.push(10);
|
|
195
205
|
expect(queue.elements.length).toBe(10);
|
|
196
|
-
while (queue.
|
|
197
|
-
expect(queue.
|
|
206
|
+
while (queue.length > 7) queue.shift();
|
|
207
|
+
expect(queue.length).toBe(7);
|
|
198
208
|
expect(queue.elements.length).toBe(10);
|
|
199
209
|
queue.shift();
|
|
200
|
-
expect(queue.
|
|
210
|
+
expect(queue.length).toBe(6);
|
|
201
211
|
expect(queue.elements.length).toBe(6);
|
|
202
212
|
});
|
|
203
213
|
});
|
|
@@ -260,7 +270,7 @@ describe('Queue - Additional Methods', () => {
|
|
|
260
270
|
queue.push(1);
|
|
261
271
|
queue.push(2);
|
|
262
272
|
expect(queue.last).toBe(2);
|
|
263
|
-
expect(queue.
|
|
273
|
+
expect(queue.length).toBe(2);
|
|
264
274
|
});
|
|
265
275
|
|
|
266
276
|
it('peekLast should return undefined if queue is empty', () => {
|
|
@@ -288,12 +298,101 @@ describe('Queue - Additional Methods', () => {
|
|
|
288
298
|
});
|
|
289
299
|
});
|
|
290
300
|
|
|
301
|
+
describe('Queue Additional Methods', () => {
|
|
302
|
+
// Slice method implementation and test
|
|
303
|
+
test('slice should return a new list with specified range', () => {
|
|
304
|
+
const list = new Queue([1, 2, 3, 4, 5]);
|
|
305
|
+
const slicedList = list.slice(1, 4);
|
|
306
|
+
|
|
307
|
+
expect(slicedList.toArray()).toEqual([2, 3, 4]);
|
|
308
|
+
expect(list.length).toBe(5); // Original list unchanged
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
// Splice method implementation
|
|
312
|
+
test('splice should modify list and return removed elements', () => {
|
|
313
|
+
const list = new Queue([1, 2, 3, 4, 5]);
|
|
314
|
+
const removedList = list.splice(2, 2, 6, 7);
|
|
315
|
+
|
|
316
|
+
expect(list.toArray()).toEqual([1, 2, 6, 7, 5]);
|
|
317
|
+
expect(removedList.toArray()).toEqual([3, 4]);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Concat method test
|
|
321
|
+
test('concat should combine multiple lists', () => {
|
|
322
|
+
const list1 = new Queue([1, 2]);
|
|
323
|
+
const list2 = new Queue([3, 4]);
|
|
324
|
+
const list3 = new Queue([5, 6]);
|
|
325
|
+
|
|
326
|
+
const concatenatedList = list1.concat(list2, list3);
|
|
327
|
+
expect(concatenatedList.toArray()).toEqual([1, 2, 3, 4, 5, 6]);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Sort method test
|
|
331
|
+
test('sort should order elements in ascending order', () => {
|
|
332
|
+
const list = new Queue([5, 2, 8, 1, 9]);
|
|
333
|
+
list.sort((a, b) => a - b);
|
|
334
|
+
|
|
335
|
+
expect(list.toArray()).toEqual([1, 2, 5, 8, 9]);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
// Reverse method test
|
|
339
|
+
test('reverse should invert the list order', () => {
|
|
340
|
+
const list = new Queue([1, 2, 3, 4, 5]);
|
|
341
|
+
list.reverse();
|
|
342
|
+
|
|
343
|
+
expect(list.toArray()).toEqual([5, 4, 3, 2, 1]);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// Join method test
|
|
347
|
+
test('join should convert list to string with separator', () => {
|
|
348
|
+
const list = new Queue(['a', 'b', 'c']);
|
|
349
|
+
|
|
350
|
+
expect(list.join('-')).toBe('a-b-c');
|
|
351
|
+
expect(list.join()).toBe('a,b,c');
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// IndexOf method test
|
|
355
|
+
test('indexOf should return first occurrence index', () => {
|
|
356
|
+
const list = new Queue([1, 2, 3, 2, 1]);
|
|
357
|
+
|
|
358
|
+
expect(list.indexOf(2)).toBe(1);
|
|
359
|
+
expect(list.indexOf(4)).toBe(-1);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// LastIndexOf method test
|
|
363
|
+
test('lastIndexOf should return last occurrence index', () => {
|
|
364
|
+
const list = new Queue([1, 2, 3, 2, 1]);
|
|
365
|
+
|
|
366
|
+
expect(list.lastIndexOf(2)).toBe(3);
|
|
367
|
+
expect(list.lastIndexOf(4)).toBe(-1);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// findIndex method test
|
|
371
|
+
test('findIndex should return first occurrence index', () => {
|
|
372
|
+
const list = new Queue([1, 2, 3, 2, 1]);
|
|
373
|
+
expect(list.findIndex(item => item === 2)).toBe(1);
|
|
374
|
+
expect(list.findIndex(item => item === 4)).toBe(-1);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
// fill method test
|
|
378
|
+
test('fill should return fill all the list', () => {
|
|
379
|
+
let list = new Queue([1, 2, 3, 2, 1]);
|
|
380
|
+
expect([...list.fill(9)]).toEqual([9, 9, 9, 9, 9]);
|
|
381
|
+
list = new Queue([1, 2, 3, 2, 1]);
|
|
382
|
+
expect([...list.fill(9, 2, 3)]).toEqual([1, 2, 9, 2, 1]);
|
|
383
|
+
list = new Queue([1, 2, 3, 2, 1]);
|
|
384
|
+
expect([...list.fill(9, -3, -2)]).toEqual([1, 2, 9, 2, 1]);
|
|
385
|
+
list = new Queue([1, 2, 3, 2, 1]);
|
|
386
|
+
expect([...list.fill(9, -2, -3)]).toEqual([1, 2, 3, 2, 1]);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
291
390
|
describe('Queue - Static and Clone Methods', () => {
|
|
292
391
|
it('fromArray should create a new queue from an array', () => {
|
|
293
392
|
const array = [1, 2, 3];
|
|
294
393
|
const queue = Queue.fromArray(array);
|
|
295
394
|
expect(queue.toArray()).toEqual(array);
|
|
296
|
-
expect(queue.
|
|
395
|
+
expect(queue.length).toBe(array.length);
|
|
297
396
|
});
|
|
298
397
|
|
|
299
398
|
it('fromArray should create an empty queue from an empty array', () => {
|
|
@@ -308,7 +407,7 @@ describe('Queue - Static and Clone Methods', () => {
|
|
|
308
407
|
|
|
309
408
|
const clonedQueue = originalQueue.clone();
|
|
310
409
|
expect(clonedQueue.toArray()).toEqual(originalQueue.toArray());
|
|
311
|
-
expect(clonedQueue.
|
|
410
|
+
expect(clonedQueue.length).toBe(originalQueue.length);
|
|
312
411
|
});
|
|
313
412
|
|
|
314
413
|
it('clone should not affect the original queue when mutated', () => {
|
|
@@ -319,7 +418,7 @@ describe('Queue - Static and Clone Methods', () => {
|
|
|
319
418
|
const clonedQueue = originalQueue.clone();
|
|
320
419
|
clonedQueue.push(3);
|
|
321
420
|
|
|
322
|
-
expect(clonedQueue.
|
|
421
|
+
expect(clonedQueue.length).not.toBe(originalQueue.length);
|
|
323
422
|
expect(originalQueue.toArray()).not.toContain(3);
|
|
324
423
|
});
|
|
325
424
|
});
|
|
@@ -335,14 +434,14 @@ describe('LinkedListQueue', () => {
|
|
|
335
434
|
|
|
336
435
|
it('should push elements to the end of the queue', () => {
|
|
337
436
|
expect(queue.first).toBe('A');
|
|
338
|
-
expect(queue.
|
|
437
|
+
expect(queue.length).toBe(2);
|
|
339
438
|
});
|
|
340
439
|
|
|
341
440
|
it('should shift elements from the front of the queue', () => {
|
|
342
441
|
const dequeued = queue.shift();
|
|
343
442
|
expect(dequeued).toBe('A');
|
|
344
443
|
expect(queue.first).toBe('B');
|
|
345
|
-
expect(queue.
|
|
444
|
+
expect(queue.length).toBe(1);
|
|
346
445
|
});
|
|
347
446
|
|
|
348
447
|
it('should peek at the front of the queue', () => {
|
|
@@ -352,6 +451,220 @@ describe('LinkedListQueue', () => {
|
|
|
352
451
|
it('should clone method work correctly', () => {
|
|
353
452
|
const cloned = queue.clone();
|
|
354
453
|
expect(cloned instanceof LinkedListQueue).toBe(true);
|
|
355
|
-
expect(cloned.
|
|
454
|
+
expect(cloned.length).toBe(2);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
describe('Queue', () => {
|
|
459
|
+
// Test queue initialization
|
|
460
|
+
it('should initialize correctly with no elements', () => {
|
|
461
|
+
const queue = new Queue();
|
|
462
|
+
expect(queue.isEmpty()).toBe(true);
|
|
463
|
+
expect(queue.length).toBe(0);
|
|
464
|
+
expect(queue.first).toBeUndefined();
|
|
465
|
+
expect(queue.last).toBeUndefined();
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it('should initialize correctly with given elements', () => {
|
|
469
|
+
const queue = new Queue([1, 2, 3]);
|
|
470
|
+
expect(queue.length).toBe(3);
|
|
471
|
+
expect(queue.first).toBe(1);
|
|
472
|
+
expect(queue.last).toBe(3);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
// Test push and pushMany
|
|
476
|
+
it('should add elements to the queue', () => {
|
|
477
|
+
const queue = new Queue<number>();
|
|
478
|
+
queue.push(1);
|
|
479
|
+
queue.push(2);
|
|
480
|
+
expect(queue.length).toBe(2);
|
|
481
|
+
expect(queue.first).toBe(1);
|
|
482
|
+
expect(queue.last).toBe(2);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('should add multiple elements using pushMany', () => {
|
|
486
|
+
const queue = new Queue<number>();
|
|
487
|
+
queue.pushMany([1, 2, 3]);
|
|
488
|
+
expect(queue.length).toBe(3);
|
|
489
|
+
expect(queue.elements).toEqual([1, 2, 3]);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// Test shift
|
|
493
|
+
it('should remove the first element from the queue', () => {
|
|
494
|
+
const queue = new Queue([1, 2, 3]);
|
|
495
|
+
const shifted = queue.shift();
|
|
496
|
+
expect(shifted).toBe(1);
|
|
497
|
+
expect(queue.length).toBe(2);
|
|
498
|
+
expect(queue.first).toBe(2);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
// Test delete and deleteAt
|
|
502
|
+
it('should delete an element from the queue', () => {
|
|
503
|
+
const queue = new Queue([1, 2, 3]);
|
|
504
|
+
const result = queue.delete(2);
|
|
505
|
+
expect(result).toBe(true);
|
|
506
|
+
expect(queue.elements).toEqual([1, 3]);
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it('should delete an element at a specific index', () => {
|
|
510
|
+
const queue = new Queue([1, 2, 3]);
|
|
511
|
+
const deleted = queue.deleteAt(1);
|
|
512
|
+
expect(deleted).toBe(2);
|
|
513
|
+
expect(queue.elements).toEqual([1, 3]);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
// Test at
|
|
517
|
+
it('should retrieve an element by index', () => {
|
|
518
|
+
const queue = new Queue([1, 2, 3]);
|
|
519
|
+
expect(queue.at(0)).toBe(1);
|
|
520
|
+
expect(queue.at(2)).toBe(3);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
// Test reverse
|
|
524
|
+
it('should reverse the queue', () => {
|
|
525
|
+
const queue = new Queue([1, 2, 3]);
|
|
526
|
+
queue.reverse();
|
|
527
|
+
expect(queue.elements).toEqual([3, 2, 1]);
|
|
528
|
+
expect(queue.first).toBe(3);
|
|
529
|
+
expect(queue.last).toBe(1);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
// Test addAt
|
|
533
|
+
it('should add an element at a specific index', () => {
|
|
534
|
+
const queue = new Queue([1, 3]);
|
|
535
|
+
const result = queue.addAt(1, 2);
|
|
536
|
+
expect(result).toBe(true);
|
|
537
|
+
expect(queue.elements).toEqual([1, 2, 3]);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
// Test setAt
|
|
541
|
+
it('should set an element at a specific index', () => {
|
|
542
|
+
const queue = new Queue([1, 2, 3]);
|
|
543
|
+
const result = queue.setAt(1, 10);
|
|
544
|
+
expect(result).toBe(true);
|
|
545
|
+
expect(queue.elements).toEqual([1, 10, 3]);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
// Test clear
|
|
549
|
+
it('should clear the queue', () => {
|
|
550
|
+
const queue = new Queue([1, 2, 3]);
|
|
551
|
+
queue.clear();
|
|
552
|
+
expect(queue.isEmpty()).toBe(true);
|
|
553
|
+
expect(queue.length).toBe(0);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
// Test compact
|
|
557
|
+
it('should compact the queue', () => {
|
|
558
|
+
const queue = new Queue([1, 2, 3]);
|
|
559
|
+
queue.shift();
|
|
560
|
+
queue.shift();
|
|
561
|
+
queue.compact();
|
|
562
|
+
expect(queue.elements).toEqual([3]);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
// Test splice
|
|
566
|
+
it('should splice elements from the queue', () => {
|
|
567
|
+
const queue = new Queue([1, 2, 3, 4]);
|
|
568
|
+
const removed = queue.splice(1, 2);
|
|
569
|
+
expect(removed.elements).toEqual([2, 3]);
|
|
570
|
+
expect(queue.elements).toEqual([1, 4]);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
// Test clone
|
|
574
|
+
it('should create a clone of the queue', () => {
|
|
575
|
+
const queue = new Queue([1, 2, 3]);
|
|
576
|
+
const clone = queue.clone();
|
|
577
|
+
expect(clone.elements).toEqual(queue.elements);
|
|
578
|
+
clone.push(4);
|
|
579
|
+
expect(queue.elements).not.toContain(4);
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
// Test filter
|
|
583
|
+
it('should filter elements based on a predicate', () => {
|
|
584
|
+
const queue = new Queue([1, 2, 3, 4]);
|
|
585
|
+
const filtered = queue.filter(el => el % 2 === 0);
|
|
586
|
+
expect(filtered.elements).toEqual([2, 4]);
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
// Test map
|
|
590
|
+
it('should map elements to a new queue', () => {
|
|
591
|
+
const queue = new Queue([1, 2, 3]);
|
|
592
|
+
const mapped = queue.map(el => el * 2);
|
|
593
|
+
expect(mapped.elements).toEqual([2, 4, 6]);
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
describe('classic uses', () => {
|
|
598
|
+
it('@example Sliding Window using Queue', () => {
|
|
599
|
+
const nums = [2, 3, 4, 1, 5];
|
|
600
|
+
const k = 2;
|
|
601
|
+
const queue = new Queue<number>();
|
|
602
|
+
|
|
603
|
+
let maxSum = 0;
|
|
604
|
+
let currentSum = 0;
|
|
605
|
+
|
|
606
|
+
nums.forEach((num, i) => {
|
|
607
|
+
queue.push(num);
|
|
608
|
+
currentSum += num;
|
|
609
|
+
|
|
610
|
+
if (queue.length > k) {
|
|
611
|
+
currentSum -= queue.shift()!;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
if (queue.length === k) {
|
|
615
|
+
maxSum = Math.max(maxSum, currentSum);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
expect(maxSum).toBe(7); // Maximum sum is from subarray [3, 4].
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it('@example Breadth-First Search (BFS) using Queue', () => {
|
|
623
|
+
const graph: { [key in number]: number[] } = {
|
|
624
|
+
1: [2, 3],
|
|
625
|
+
2: [4, 5],
|
|
626
|
+
3: [],
|
|
627
|
+
4: [],
|
|
628
|
+
5: []
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
const queue = new Queue<number>();
|
|
632
|
+
const visited: number[] = [];
|
|
633
|
+
|
|
634
|
+
queue.push(1);
|
|
635
|
+
|
|
636
|
+
while (!queue.isEmpty()) {
|
|
637
|
+
const node = queue.shift()!;
|
|
638
|
+
if (!visited.includes(node)) {
|
|
639
|
+
visited.push(node);
|
|
640
|
+
graph[node].forEach(neighbor => queue.push(neighbor));
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
expect(visited).toEqual([1, 2, 3, 4, 5]); // Expected BFS traversal order.
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it('Task Scheduling using Queue', () => {
|
|
648
|
+
const tasks = ['A', 'A', 'A', 'B', 'B', 'B'];
|
|
649
|
+
const cooldown = 2;
|
|
650
|
+
|
|
651
|
+
const taskQueue = new Queue<string>();
|
|
652
|
+
const cooldownQueue = new Queue<string>();
|
|
653
|
+
|
|
654
|
+
for (const task of tasks) {
|
|
655
|
+
while (!cooldownQueue.isEmpty() && cooldownQueue.first === task) {
|
|
656
|
+
cooldownQueue.shift();
|
|
657
|
+
taskQueue.push('idle');
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
taskQueue.push(task);
|
|
661
|
+
cooldownQueue.push(task);
|
|
662
|
+
if (cooldownQueue.length > cooldown) {
|
|
663
|
+
cooldownQueue.shift();
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
const scheduled = taskQueue.elements;
|
|
668
|
+
expect(scheduled).toEqual(['A', 'idle', 'A', 'idle', 'A', 'B', 'B', 'idle', 'idle', 'B']);
|
|
356
669
|
});
|
|
357
670
|
});
|
|
@@ -162,3 +162,168 @@ describe('Stack iterative methods', () => {
|
|
|
162
162
|
}).toThrow('toElementFn must be a function type');
|
|
163
163
|
});
|
|
164
164
|
});
|
|
165
|
+
|
|
166
|
+
describe('classic uses', () => {
|
|
167
|
+
it('@example Balanced Parentheses or Brackets', () => {
|
|
168
|
+
type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
169
|
+
|
|
170
|
+
const stack = new Stack<string>();
|
|
171
|
+
const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
172
|
+
const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
173
|
+
for (const char of input) {
|
|
174
|
+
if ('([{'.includes(char)) {
|
|
175
|
+
stack.push(char);
|
|
176
|
+
} else if (')]}'.includes(char)) {
|
|
177
|
+
if (stack.pop() !== matches[char]) {
|
|
178
|
+
fail('Parentheses are not balanced');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
expect(stack.isEmpty()).toBe(true);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('@example Expression Evaluation and Conversion', () => {
|
|
186
|
+
const stack = new Stack<number>();
|
|
187
|
+
const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
188
|
+
expression.forEach(token => {
|
|
189
|
+
if (typeof token === 'number') {
|
|
190
|
+
stack.push(token);
|
|
191
|
+
} else {
|
|
192
|
+
const b = stack.pop()!;
|
|
193
|
+
const a = stack.pop()!;
|
|
194
|
+
stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
expect(stack.pop()).toBe(8);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('@example Depth-First Search (DFS)', () => {
|
|
201
|
+
const stack = new Stack<number>();
|
|
202
|
+
const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
203
|
+
const visited: number[] = [];
|
|
204
|
+
stack.push(1);
|
|
205
|
+
while (!stack.isEmpty()) {
|
|
206
|
+
const node = stack.pop()!;
|
|
207
|
+
if (!visited.includes(node)) {
|
|
208
|
+
visited.push(node);
|
|
209
|
+
graph[node].forEach(neighbor => stack.push(neighbor));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
expect(visited).toEqual([1, 3, 5, 2, 4]); // Example DFS order
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('@example Backtracking Algorithms', () => {
|
|
216
|
+
const stack = new Stack<[number, number]>();
|
|
217
|
+
const maze = [
|
|
218
|
+
['S', ' ', 'X'],
|
|
219
|
+
['X', ' ', 'X'],
|
|
220
|
+
[' ', ' ', 'E']
|
|
221
|
+
];
|
|
222
|
+
const start: [number, number] = [0, 0];
|
|
223
|
+
const end = [2, 2];
|
|
224
|
+
const directions = [
|
|
225
|
+
[0, 1], // To the right
|
|
226
|
+
[1, 0], // down
|
|
227
|
+
[0, -1], // left
|
|
228
|
+
[-1, 0] // up
|
|
229
|
+
];
|
|
230
|
+
|
|
231
|
+
const visited = new Set<string>(); // Used to record visited nodes
|
|
232
|
+
stack.push(start);
|
|
233
|
+
const path: number[][] = [];
|
|
234
|
+
|
|
235
|
+
while (!stack.isEmpty()) {
|
|
236
|
+
const [x, y] = stack.pop()!;
|
|
237
|
+
if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
238
|
+
visited.add(`${x},${y}`);
|
|
239
|
+
|
|
240
|
+
path.push([x, y]);
|
|
241
|
+
|
|
242
|
+
if (x === end[0] && y === end[1]) {
|
|
243
|
+
break; // Find the end point and exit
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
for (const [dx, dy] of directions) {
|
|
247
|
+
const nx = x + dx;
|
|
248
|
+
const ny = y + dy;
|
|
249
|
+
if (
|
|
250
|
+
maze[nx]?.[ny] === ' ' || // feasible path
|
|
251
|
+
maze[nx]?.[ny] === 'E' // destination
|
|
252
|
+
) {
|
|
253
|
+
stack.push([nx, ny]);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
expect(path).toContainEqual(end);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('@example Function Call Stack', () => {
|
|
262
|
+
const functionStack = new Stack<string>();
|
|
263
|
+
functionStack.push('main');
|
|
264
|
+
functionStack.push('foo');
|
|
265
|
+
functionStack.push('bar');
|
|
266
|
+
expect(functionStack.pop()).toBe('bar');
|
|
267
|
+
expect(functionStack.pop()).toBe('foo');
|
|
268
|
+
expect(functionStack.pop()).toBe('main');
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('@example Simplify File Paths', () => {
|
|
272
|
+
const stack = new Stack<string>();
|
|
273
|
+
const path = '/a/./b/../../c';
|
|
274
|
+
path.split('/').forEach(segment => {
|
|
275
|
+
if (segment === '..') stack.pop();
|
|
276
|
+
else if (segment && segment !== '.') stack.push(segment);
|
|
277
|
+
});
|
|
278
|
+
expect(stack.elements.join('/')).toBe('c');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('@example Stock Span Problem', () => {
|
|
282
|
+
const stack = new Stack<number>();
|
|
283
|
+
const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
284
|
+
const spans: number[] = [];
|
|
285
|
+
prices.forEach((price, i) => {
|
|
286
|
+
while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
287
|
+
stack.pop();
|
|
288
|
+
}
|
|
289
|
+
spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
290
|
+
stack.push(i);
|
|
291
|
+
});
|
|
292
|
+
expect(spans).toEqual([1, 1, 1, 2, 1, 4, 6]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('Browser Navigation', () => {
|
|
296
|
+
const backStack = new Stack<string>();
|
|
297
|
+
const forwardStack = new Stack<string>();
|
|
298
|
+
backStack.push('Page 1');
|
|
299
|
+
backStack.push('Page 2');
|
|
300
|
+
forwardStack.push(backStack.pop()!);
|
|
301
|
+
expect(backStack.size).toBe(1);
|
|
302
|
+
expect(forwardStack.size).toBe(1);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('String Reversal', () => {
|
|
306
|
+
const stack = new Stack<string>();
|
|
307
|
+
const input = 'hello';
|
|
308
|
+
const reversed = [];
|
|
309
|
+
input.split('').forEach(char => stack.push(char));
|
|
310
|
+
while (!stack.isEmpty()) {
|
|
311
|
+
reversed.push(stack.pop());
|
|
312
|
+
}
|
|
313
|
+
expect(reversed.join('')).toBe('olleh');
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('Next Greater Element', () => {
|
|
317
|
+
const stack = new Stack<number>();
|
|
318
|
+
const array = [4, 5, 2, 25];
|
|
319
|
+
const nextGreater = new Array(array.length).fill(-1);
|
|
320
|
+
array.forEach((_, i) => {
|
|
321
|
+
while (!stack.isEmpty() && array[stack.peek()!] < array[i]) {
|
|
322
|
+
const idx = stack.pop()!;
|
|
323
|
+
nextGreater[idx] = array[i];
|
|
324
|
+
}
|
|
325
|
+
stack.push(i);
|
|
326
|
+
});
|
|
327
|
+
expect(nextGreater).toEqual([5, 25, 25, -1]);
|
|
328
|
+
});
|
|
329
|
+
});
|