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.
i am getting time out after case6(included) excluding 10,11. A little optimization hint would be great thanks.
classMyQueue:def__init__(self):self.stack=[]self.stack2=[]self.flag=1#to check after subsequent dequeue and peek operations, enqueue has been called again deftransfer1to2(self):while(self.stack):self.stack2.append(self.popp(1))deftransfer2to1(self):while(self.stack2):self.stack.append(self.popp())defenqueue(self,a):if(self.flag==0):self.transfer2to1()self.flag=1self.stack.append(a)defdequeue(self):self.transfer1to2()self.stack2.pop()self.flag=0defpeek(self):self.transfer1to2()self.flag=0returnself.stack2[-1]defpopp(self,a=2):if(a==1):if(self.stack):returnself.stack.pop()else:if(self.stack2):returnself.stack2.pop()if__name__=="__main__":queue=MyQueue()foriinrange(int(input())):value=list(map(int,input().split()))ifvalue[0]==1:queue.enqueue(value[1])ifvalue[0]==2:queue.dequeue()ifvalue[0]==3: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 →
i am getting time out after case6(included) excluding 10,11. A little optimization hint would be great thanks.