You are viewing a single comment's thread. Return to all comments →
JavaScript Solution:-
function processData(input) { let queries = input.split('\n'); let q = parseInt(queries[0]); let stack1 = []; let stack2 = []; for (let i = 1; i <= q; i++) { let query = queries[i].split(' '); let type = parseInt(query[0]); if (type === 1) { let x = parseInt(query[1]); stack1.push(x); } else if (type === 2) { if (stack2.length === 0) { while (stack1.length > 0) { stack2.push(stack1.pop()); } } stack2.pop(); } else if (type === 3) { if (stack2.length === 0) { while (stack1.length > 0) { stack2.push(stack1.pop()); } } console.log(stack2[stack2.length - 1]); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
JavaScript Solution:-