Queue using Two Stacks

  • + 0 comments

    JS

    Barely able to pass without the timeout error after trying multiple times:

    function processData(input) {
        let s1 = [], s2 = []
        let inputs = input.split('\n')
        for (let i = 1; i < inputs.length; i++) {
            let arr = inputs[i].split(' ')
            switch (arr[0]) {
                case '1': {
                    s1.push(arr[1])
                    break
                }
                case '2': {
                    while (s1.length > 0) s2.push(s1.pop())
                    s2.pop()
                    while (s2.length > 0) s1.push(s2.pop())
                    break
                }
                case '3': {
                    console.log(s1[0])
                    break
                }
            }
        }
    }