Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-174

Queue from Two Stacks Amortized Cost Count

Problem

Count the element moves when a queue is built from two stacks.

operations contains "push", "pop", or "peek"; values provides pushed values. Elements enter an inbox stack and are transferred to an outbox stack only when an outbox operation finds it empty, and every transferred element counts as one move.

Return the total number of moves after all operations.

Hint

Each element is transferred from inbox to outbox at most once, giving amortized O(1) operations.

Test cases

case 1
in: [["push","push","push","pop","pop","pop"],[1,2,3,0,0,0]]
expected: 3
case 2
in: [["push","pop","push","pop","push","pop"],[1,0,2,0,3,0]]
expected: 3
case 3
in: [["pop"],[0]]
expected: 0
case 4
in: [["push","peek","pop"],[7,0,0]]
expected: 1
Starter codePython
def two_stack_queue_moves(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor