Software Engineering bootcamp graduate currently seeking work. Previously a senior manufacturing specialist with over 4 years in the Biotechnology industry.
I did implement the peek method in my stack to see the last value in the array.
I'm not sure if I am understanding your implementation right, but I think the way you're thinking about the problem is the way I originally thought of it as well. However say you have this stack [1, 2, 1, 3]. In this case, 3 would be your current stored Max. Now if you pop 3 off and just peek the last element, your code would assume the new max is 1, however it's actually 2.
This is why using another stack is helpful. With my code, the max stack would look like this `[1, 2, 2, 3]'. Now when 3 is popped off the stack, my code will also pop the last value of the max stack. Now we have:
//current stack
[1, 2, 1]
//max stack
[1, 2, 2]
Now all we have to do is peek the last element of the max stack and know that the current max is 2.
Thanks for your input and let me know if I cleared it up a bit.
No no, I do understand your solution fully. What I didn't make clear though is that my solution assumes that a stack is already hydrated when it gets the max value, and your solution is perfect for getting the max value without traversing the stack each time max is required. I'm not sure if you get me
Software Engineering bootcamp graduate currently seeking work. Previously a senior manufacturing specialist with over 4 years in the Biotechnology industry.
Oh I see. I did misunderstand at first, I apologize for that. So your solution is a loop, but with pushing to a holding stack while comparing values? This also assumes that no new values are added to the stack. Am I understanding it correctly?
Hi Beautus,
Thank you for reading!
I did implement the peek method in my stack to see the last value in the array.
I'm not sure if I am understanding your implementation right, but I think the way you're thinking about the problem is the way I originally thought of it as well. However say you have this stack
[1, 2, 1, 3]
. In this case, 3 would be your current stored Max. Now if you pop 3 off and just peek the last element, your code would assume the new max is 1, however it's actually 2.This is why using another stack is helpful. With my code, the max stack would look like this `[1, 2, 2, 3]'. Now when 3 is popped off the stack, my code will also pop the last value of the max stack. Now we have:
//current stack
[1, 2, 1]
//max stack
[1, 2, 2]
Now all we have to do is peek the last element of the max stack and know that the current max is 2.
Thanks for your input and let me know if I cleared it up a bit.
No no, I do understand your solution fully. What I didn't make clear though is that my solution assumes that a stack is already hydrated when it gets the max value, and your solution is perfect for getting the max value without traversing the stack each time max is required. I'm not sure if you get me
Oh I see. I did misunderstand at first, I apologize for that. So your solution is a loop, but with pushing to a holding stack while comparing values? This also assumes that no new values are added to the stack. Am I understanding it correctly?
Yes that definitely it