Queue using Two Stacks

Sort by

recency

|

253 Discussions

|

  • + 0 comments

    My C# solution

    Spent ages trying to find input from args when it actually comes from Console.ReadLine. Had to read up on two stacks before attempting question. Was failing a few test cases on time limit until I switched from using linq .Any() to checking .Count > 0

    class Solution {
        static void Main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
            (Stack<int> stack1, Stack<int> stack2) = (new(), new());
    
            var numberOfInputs = Int32.Parse(Console.ReadLine()!);
            
            for (int _ = 0; _ < numberOfInputs; _++)
            {
                var arg = Console.ReadLine()!.Split();
                switch(arg[0])
                {
                    case "1":
                        var enqueueValue = Int32.Parse(arg[1]);
                        stack1.Push(enqueueValue);
                        continue;
                    case "2":
                    case "3":
                        if (stack2.Count == 0) 
                        {
                            while(stack1.Count > 0)
                            {
                                stack2.Push(stack1.Pop());
                            }
                        }
                        if (arg[0] == "3")
                        {
                            Console.WriteLine(stack2.Count > 0 ? stack2.Peek() : stack1.Peek());
                            continue; 
                        }
                        stack2.Pop(); 
                        continue;
                    default:
                      throw new ArgumentException("Ffs Hackerrank");
                }
            }
        }
    }
    
  • + 1 comment

    uh, does this even work on C#? It's not passing me any args

  • + 0 comments

    js answer using solely regex to parse the input (and actually 2 'stacks'):

    function processData(input) {
        let frontStack = new Array();
        let tailStack = new Array();
        let regex = /(?<=\n)(?<type>\d)\s*?(?<value>\d*)(?=(\n|$))/g;
        let inputObjArr = [...input.matchAll(regex)].map((result) => (
            {
                'type': Number(result.groups.type),
                'value': result.groups.value
            }
        ));
        inputObjArr.forEach((inputObj) => {
            switch(inputObj.type){
                case 1: {
                    tailStack.push(inputObj.value);
                    break;   
                }
                case 2: {
                    if(frontStack.length === 0){
                        while(tailStack.length > 0){
                            frontStack.push(tailStack.pop());
                        }
                    }
                    frontStack.pop();
                    break;
                }
                case 3: {
                    if(frontStack.length === 0){
                        while(tailStack.length > 0){
                            frontStack.push(tailStack.pop());
                        }
                    }
                    console.log(frontStack.at(-1))
                    break;
                }
            }
        })
    } 
    
  • + 0 comments
    p=[]
    for _ in range(int(input())):
        l=list(map(int,input().split()))
        if l[0]==1:
            p.append(l[1])
        if l[0]==2:
            p.pop(0)
        if l[0]==3:
            print(p[0])
        
            
    
  • + 0 comments

    My Solution in Javascript also works with TypeScript:

    function processData(input) {

    const query = input.split('\n').slice(1)
    
    const answer = []
    
    for(let i = 0; i < query.length; i++) {
        if(query[i].charAt(0) === '1') {
            const [index, number] = query[i].split(' ')
            answer.push(number) 
        }
        if(query[i].charAt(0) === '2') {
            answer.shift()
        }
    
        if(query[i].charAt(0) === '3') {
            console.log(answer[0])
        }
    }
    

    }