We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classMyQueue(object):def__init__(self):self.stack1=[]self.stack2=[]defpeek(self):iflen(self.stack2)>0:top=self.stack2.pop()self.stack2.append(top)else:while(len(self.stack1)>1):self.stack2.append(self.stack1.pop())top=self.stack1.pop()self.stack2.append(top)returntopdefpop(self):iflen(self.stack2)>0:self.stack2.pop()else:while(len(self.stack1)>1):self.stack2.append(self.stack1.pop())self.stack1.pop()#stack1 is now emptydefput(self,value):self.stack1.append(value)queue=MyQueue()t=int(input())forlineinrange(t):values=map(int,input().split())values=list(values)ifvalues[0]==1:queue.put(values[1])elifvalues[0]==2:queue.pop()else:print(queue.peek())
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queues: A Tale of Two Stacks
You are viewing a single comment's thread. Return to all comments →
Here is my Python3 solution (100%, no timeouts):