We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I think for this task creation of additional class is needless. You could just store max int value at the stack during push, since we anyway don't use stack values other than getting the max. Thus, with every push the top of the stack will contain max element, popping element will bring you to the previous max element.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
I think for this task creation of additional class is needless. You could just store max int value at the stack during push, since we anyway don't use stack values other than getting the max. Thus, with every push the top of the stack will contain max element, popping element will bring you to the previous max element.