-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_using_two_Stacks.py
45 lines (36 loc) · 1.15 KB
/
Queue_using_two_Stacks.py
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
"""
Implement a Queue using 2 stacks s1 and s2 .
You are required to complete the two methods push which take one argument an integer 'x' to be pushed into the queue
and pop which returns a integer poped out from other queue(-1 if the queue is empty).
Expected Time Complexity : O(1) for push() and O(N) for pop() or O(N) for push() and O(1) for pop()
Expected Auxilliary Space : O(1).
"""
# first solution: Time Complexity : O(1) for push() and O(N) for pop()
def Push(x, stack1, stack2):
'''
x: value to push, stack1: list, stack2: list
'''
stack1.append(x)
def Pop(stack1, stack2):
'''
stack1: list, stack2: list
'''
if not stack1:
return -1
while stack1:
stack2.append(stack1.pop(-1))
popped = stack2.pop(-1)
while stack2:
stack1.append(stack2.pop(-1))
return popped
# second solution: Time Complexity : O(N) for push() and O(1) for pop()
def Push(x, stack1, stack2):
while stack1:
stack2.append(stack1.pop(-1))
stack1.append(x)
while stack2:
stack1.append(stack2.pop(-1))
def Pop(stack1, stack2):
if not stack1:
return -1
return stack1.pop(-1)