Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Leszek Kalwa
Javascript solution

Leszek Kalwa

Sep 28, 2024

Guys,

Either you treat stack as a stack, or as an array. What the heck is line:15 from your js solution? That's clearly accessing something by index.

// While the temporary stack is not empty and the top element // of the temporary stack is greater than the current element, // push elements from the temporary stack back to the original stack. while (tempStack.length && tempStack[tempStack.length - 1] > temp) { stack.push(tempStack.pop()); }

2

0

Comments
Comments
tai
tai a year ago

I ended up using tempStack.at(-1), sometimes the solution was accepted and sometimes it wasn't - but I agree, javascript arrays don't technically have a "peek" function so I feel like the actual solution would require more lines as you'd need to pop off the top and then...

On this page