forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_053.js
41 lines (36 loc) Β· 876 Bytes
/
problem_053.js
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
// https://leetcode.com/problems/implement-queue-using-stacks/description/
// Algorithmic Paradigm: Data Structures
// Enqueue: O(1) Time complexity
// Dequeue: Amortized O(1) Time complexity
class Queue {
/**
* Initialize an empty queue, with two stacks. FIFO(first-in, first-out)
* Enqueue all items on stack1, stack2 gets used in dequeue
*/
constructor() {
this.stack1 = [];
this.stack2 = [];
}
/**
* Inserts an element into the queue
* @param {*} value
*/
enqueue(value) {
this.stack1.push(value);
}
/**
* Removes an element into the queue
* @return {*}
*/
dequeue() {
if(this.stack1.length === 0 && this.stack2.length === 0) return null;
// Push contents into stack2
if(this.stack2.length === 0) {
while(this.stack1.length !== 0) {
let num = this.stack1.pop();
this.stack2.push(num);
}
}
return this.stack2.pop();
}
}