Queue using Two Stacks

Sort by

recency

|

252 Discussions

|

  • + 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])
        }
    }
    

    }

  • + 0 comments

    Error with C languaje, only this appears when doing the problem:

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
    
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
        return 0;
    }