Closed kingstenbanh closed 6 years ago
@kingstenbanh wow, nice catch! And this is why I mentioned in the README.md and in the article, some of my solutions are less than perfect, so if anyone can improve, I'm happy to listen!
This makes total sense, though, and is a much simpler way of doing it. I had the last-in-first-out part right, but was just going about it in a very inefficient way... This is much better.
Are you interested in making a PR? If so, I would gladly accept it.
Thanks for the props, too, btw! Glad you are finding the project useful.
Since this is for educational purpose, we don't want to mislead others.
And yes, I totally agree.
I love your project! It already helps me sharpening my rusty CS skills. Stack is last in first out. With the way you push new value into the stack, a new value gets stacked below the old value instead of being on top of the stack.
O(n)
O(1)
When call
stack.pop()
,stack.push()
, orstack.peek()
, you have to recursively loop down to the last value and remove/add/peek it from/to the stack, which does not have O(1). Here is my propose solution:Since this is for educational purpose, we don't want to mislead others.