Queue using Two Stacks

Sort by

recency

|

58 Discussions

|

  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    from collections import deque
    
    stack = deque()
    stack2 = deque()
    
    if __name__ == '__main__':
        n = int(input())
        for i in range(n):
            input_value = input()
            inp = list(map(int, input_value.split()))
            if inp[0] == 1:
                while(stack2):
                    stack.append(stack2.pop())
                obj = inp[1]
                stack.append(obj)
            elif inp[0] == 2:
                while(stack):
                    stack2.append(stack.pop())
                stack2.pop()
            else:
                while(stack):
                    stack2.append(stack.pop())
                to_print = stack2.pop()
                print(to_print)
                stack2.append(to_print)
                    
    

    I'm getting time limit exceeded. Any idea?

  • + 0 comments

    if you use Queue internally who gonna know ??

  • + 0 comments

    There's a typo in the sample input.

    1 14 enqueue 42

    This line should read

    1 14 enqueue 14

  • + 0 comments
    q = []
    for s in [input() for _ in range(int(input()))]:
        q.append(s[2:]) if s[0] == '1' else q.pop(0) if s == '2' else print(q[0]) if s == '3' else ...
    
  • + 0 comments

    here is hackerrank queue using two stacks problem solution in Python, Java, C++, C and javascript