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.
To solve this challenge, we can implement a queue using two https://hometownstorageunit.com/. The primary mechanism behind this is that one stack will be used for enqueuing elements (the enqueue stack) and the other for dequeuing elements (the dequeue stack). When we want to enqueue an element, we simply push it onto the enqueue stack. However, when we want to dequeue an element or retrieve the front element, we need to ensure that the elements are in the correct order (FIFO). If the dequeue stack is empty, we pop all elements from the enqueue stack and push them onto the dequeue stack, which reverses their order. Then, the top of the dequeue stack represents the front of the queue.
Here's the Python code to implement the queue using two stacks and process the queries as per the problem statement:
classQueueWithTwoStacks:def__init__(self):self.enqueue_stack=[]self.dequeue_stack=[]deftransfer_elements(self):whileself.enqueue_stack:self.dequeue_stack.append(self.enqueue_stack.pop())defenqueue(self,x):self.enqueue_stack.append(x)defdequeue(self):ifnotself.dequeue_stack:self.transfer_elements()returnself.dequeue_stack.pop()defpeek(self):ifnotself.dequeue_stack:self.transfer_elements()returnself.dequeue_stack[-1]# Read number of queriesq=int(input())# Create a queuequeue=QueueWithTwoStacks()# Process each queryfor_inrange(q):query=input().split()query_type=int(query[0])ifquery_type==1:# Enqueue operationx=int(query[1])queue.enqueue(x)elifquery_type==2:# Dequeue operationqueue.dequeue()elifquery_type==3:# Print operationprint(queue.peek())
Let's break down the sample input and output:
We perform 10 queries on the queue.
We enqueue 42, the queue state is [42].
We dequeue the first element, queue state becomes [].
We enqueue 14, the queue state is [14].
We print the first element, output is 14.
We enqueue 28, the queue state is [14, 28].
We print the first element, output is 14.
We enqueue 60, then 78, the queue state is [14, 28, 60, 78].
We dequeue twice, removing 14 and then 28, the queue state becomes [60, 78].
The output as per the sample input would be:
1414
This code handles all the operations efficiently. The enqueue operation is (O(1)), while the dequeue and peek operations are (O(n)) in the worst case but (O(1)) amortized because elements are only moved from the enqueue stack to the dequeue stack when the dequeue stack is empty.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
To solve this challenge, we can implement a queue using two https://hometownstorageunit.com/. The primary mechanism behind this is that one stack will be used for enqueuing elements (the
enqueue
stack) and the other for dequeuing elements (thedequeue
stack). When we want to enqueue an element, we simply push it onto theenqueue
stack. However, when we want to dequeue an element or retrieve the front element, we need to ensure that the elements are in the correct order (FIFO). If thedequeue
stack is empty, we pop all elements from theenqueue
stack and push them onto thedequeue
stack, which reverses their order. Then, the top of thedequeue
stack represents the front of the queue.Here's the Python code to implement the queue using two stacks and process the queries as per the problem statement:
Let's break down the sample input and output:
42
, the queue state is[42]
.[]
.14
, the queue state is[14]
.14
.28
, the queue state is[14, 28]
.14
.60
, then78
, the queue state is[14, 28, 60, 78]
.14
and then28
, the queue state becomes[60, 78]
.The output as per the sample input would be:
This code handles all the operations efficiently. The
enqueue
operation is (O(1)), while thedequeue
andpeek
operations are (O(n)) in the worst case but (O(1)) amortized because elements are only moved from theenqueue
stack to thedequeue
stack when thedequeue
stack is empty.