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.
- Queue using Two Stacks
- Discussions
Queue using Two Stacks
Queue using Two Stacks
Sort by
recency
|
234 Discussions
|
Please Login in order to post a comment
YES I CHEATED but it was fun to see it passing all test cases
Here is my c++ solution
The idea behind moving items from stack 1 to stack 2 only when stack 2 is empty is because any item pushed unto stack 1 is the latest.
When stack 2 is empty, and stack 1 is inverted unto stack 2 the oldest item is at the top of stack 2. Stack 2 keeps the order of oldest to newer from top to bottom.
That way any item newer than bottom of stack 2 will be placed on stack 1.
This is broken, you should be able to read in the args (in java) by just accessing the array, and if the system isnt set up for that, that part of the setup shouldn't be necessary for developmetn of the solution, which is the stacks.
My Python solution: