-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathQueueUsingStacks.java
More file actions
61 lines (49 loc) · 1.79 KB
/
QueueUsingStacks.java
File metadata and controls
61 lines (49 loc) · 1.79 KB
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
// ## Problem 1: (https://leetcode.com/problems/implement-queue-using-stacks/)
// Time Complexity : Avg ~ O(1) Worst ~ O(100) since the problem mentions atmost 100 calls will be made
// Space Complexity : O(2n) ~ O(n) for 2 stacks
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
// Your code here along with comments explaining your approach
/*
* Took two stacks similar to what was explained in the class.
* When pop and peek operation is triggered and outStack is Empty, start transferring elements from inStack
* Always return pop and peek from outStack and push in inStack
*/
import java.util.*;
public class QueueUsingStacks {
Stack<Integer> inStack;
Stack<Integer> outStack;
public QueueUsingStacks() {
inStack = new Stack<>();
outStack = new Stack<>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
transferElementsFromStack();
return outStack.pop();
}
public int peek() {
transferElementsFromStack();
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void transferElementsFromStack() {
if(outStack.isEmpty()) {
while(!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
public static void main(String[] args) {
QueueUsingStacks myQueue = new QueueUsingStacks();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
System.out.println(myQueue.peek());// return 1
System.out.println(myQueue.pop()); // return 1, queue is [2]
System.out.println(myQueue.empty()); // return false
}
}