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.
Skipped this one as I didn't want to write all the reading code. Regardles the idea for the implementation
enqueue = push to inStack
dequeue = iff outStack is empty pop everything from inStack while pushing to outStack; pop from outStack
time complexity:
enqueue is constant O(1)
dequeue is also constant O(2) -- while taking any single element from the queue is linear in worst case (when outStack is empty), amortised complexity (taking N elementes) will result in 2 operations per element (one to move the element from inStack to outStack and one to take it from the outStack).
de
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 →
Skipped this one as I didn't want to write all the reading code. Regardles the idea for the implementation
time complexity: enqueue is constant O(1) dequeue is also constant O(2) -- while taking any single element from the queue is linear in worst case (when outStack is empty), amortised complexity (taking N elementes) will result in 2 operations per element (one to move the element from inStack to outStack and one to take it from the outStack).
de