Stack Using Two Queues
Mediumjavapythonccppjavascript
Implement a stack using two queues, so that operations still behave in Last In, First Out order. (Each push may take time proportional to the current size, so the number of operations is kept small.)
Input Format
The first line contains an integer Q. Each of the next Q lines is one of: push X, pop, top, empty.
Output Format
For each pop or top, print the element (or -1 if empty). For each empty, print 1 if empty, otherwise 0. One value per line.
Example 1
Input
3 push 10 push 20 pop
Output
20
Explanation: The most recently inserted value is removed first.
- 1 <= Q <= 2000
- -1000000000 <= X <= 1000000000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Next Greater ElementMedium · java, python, c, cpp, javascriptSolve Problem
- Next Smaller ElementMedium · java, python, c, cpp, javascriptSolve Problem
- Min StackMedium · java, python, c, cpp, javascriptSolve Problem