-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathProblem_3.py
More file actions
47 lines (34 loc) · 1.27 KB
/
Problem_3.py
File metadata and controls
47 lines (34 loc) · 1.27 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
'''
Here we can either use 2 queues or 1 queue.
If using 2 queues,during pop we transfer all the elements from q1 to q2
except for the last element. The last element in q1 is the item to be poped
as per the stack's design.
Once we pop the final element from q1, we swap q1 and q2 to make sure q2 stays empty
for the pop operation next time we need to do the transfer.
This makes pop O(n)
If using 1 queue, during the push we first push the incoming element
then rotate all the elements in the queue except for the last element. This makes sure
the last element is always at the top of queue.
This makes pop to be a O(1) as the item to be popped as per stack is already at the top
of the queue.
On the other hand now push becomes O(n)
'''
class MyStack:
def __init__(self):
self.q1 = deque()
def push(self, x: int) -> None:
self.q1.append(x)
for _ in range(len(self.q1) - 1):
self.q1.append(self.q1.popleft())
def pop(self) -> int:
return self.q1.popleft()
def top(self) -> int:
return self.q1[0]
def empty(self) -> bool:
return len(self.q1) == 0
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()