forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_1.py
More file actions
45 lines (32 loc) · 1.07 KB
/
Copy pathExercise_1.py
File metadata and controls
45 lines (32 loc) · 1.07 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
# Implement Queue using Stacks
# https://leetcode.com/problems/implement-queue-using-stacks/
# Time complexity: O(1)
# Space complexity: O(n)
# uses an in stack for all incoming elements; when either peek or pop operation is called, then all elements from in stack is moved to out stack;
# this way out stack will have the first element added
class MyQueue:
def __init__(self):
self.in_st = []
self.out_st = []
def push(self, x: int) -> None:
self.in_st.append(x)
def pop(self) -> int:
if self.empty():
return -1
self.peek()
return self.out_st.pop()
def peek(self) -> int:
if self.empty():
return -1
if not self.out_st:
while self.in_st:
self.out_st.append(self.in_st.pop())
return self.out_st[-1]
def empty(self) -> bool:
return not self.in_st and not self.out_st
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()