Stack and queue in Python
Stack:
>>> stack = [1,2,3,4,5,6,7] >>> stack.pop() 7 >>> stack [1, 2, 3, 4, 5, 6]
Queue:
>>> queue = [1,2,3,4,5,6,7] >>> queue.pop(0) 1 >>> queue [2, 3, 4, 5, 6, 7]
Problem: Implement a Min Stack
class MinStack: def __init__(self): self.min = None self.data = [] def push(self, x: int) -> None: self.data.append(x) self.min = x if self.min == None else min(x, self.min) def pop(self) -> None: self.data.pop() self.min = self.top() for x in self.data: if x < self.min: self.min = x def top(self) -> int: if len(self.data) == 0: return None return self.data[len(self.data) -1] def getMin(self) -> int: return self.min
same using reduce and one liners for if-else:
import functools class MinStack: def __init__(self): self.min = None self.data = [] def push(self, x: int) -> None: self.data.append(x) self.min = x if self.min == None else min(x, self.min) def pop(self) -> None: self.data.pop() self.min = functools.reduce(lambda x,y: x if x<y else y,self.data,self.top()) def top(self) -> int: return None if len(self.data) == 0 else self.data[len(self.data) -1] def getMin(self) -> int: return self.min
Problem: Implement a stack using two queues
Implement a last in first out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal queue (push
, top
, pop
, and empty
).
Implement the MyStack
class:
void push(int x)
Pushes element x to the top of the stack.int pop()
Removes the element on the top of the stack and returns it.int top()
Returns the element on the top of the stack.boolean empty()
Returnstrue
if the stack is empty,false
otherwise.
Notes:
- You must use only standard operations of a queue, which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue), as long as you use only a queue's standard operations.
Follow-up: Can you implement the stack such that each operation is amortized O(1)
time complexity? In other words, performing n
operations will take overall O(n)
time even if one of those operations may take longer.
Example 1:
Input ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []] Output [null, null, null, 2, 2, false] Explanation MyStack myStack = new MyStack(); myStack.push(1); myStack.push(2); myStack.top(); // return 2 myStack.pop(); // return 2 myStack.empty(); // return False
Constraints:
1 <= x <= 9
- At most
100
calls will be made topush
,pop
,top
, andempty
. - All the calls to
pop
andtop
are valid.
SOLUTION:
class MyStack: def __init__(self): """ Initialize your data structure here. """ self.main_queue = [] self.temp_queue = [] def push(self, x: int) -> None: """ Push element x onto stack. """ self.main_queue.append(x) def pop(self) -> int: """ Removes the element on top of the stack and returns that element. """ #move all the items but the last one to temp queue self.temp_queue = [] for i in range(0, len(self.main_queue) - 1): self.temp_queue.append(self.main_queue.pop(0)) to_return = self.main_queue.pop(0) # last item self.main_queue = [] for i in range(0, len(self.temp_queue)): self.main_queue.append(self.temp_queue.pop(0)) return to_return def top(self) -> int: """ Get the top element. """ x = self.pop() self.main_queue.append(x) return x def empty(self) -> bool: """ Returns whether the stack is empty. """ if not self.main_queue: return True # 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()