Queue and Stack Queries
EasyP3Coding
There is a queue (first in, first out) and a stack (last in, first out), both empty at the start. Process queries in order. Each query is one of:
[1, x]: addxto the back of the queue.[2, x]: pushxonto the stack.[3]: report the element at the front of the queue, or-1if the queue is empty.[4]: report the element on top of the stack, or-1if the stack is empty.[5]: remove the front element of the queue and push it onto the stack. If the queue is empty, report-1and change nothing; otherwise report nothing.
Return everything reported, in order.
Example 1
- Input
- queries = [[1,5],[1,6],[3],[5],[4],[3]]
- Output
- [5,5,6]
- Why
- Front is 5. Moving 5 to the stack makes the top 5. The new front is 6.
Constraints
- 1 <= queries.length <= 10^5
- 1 <= x <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.