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.
This is for safety to ensure we have something on the stack: s.size()>0?s.top():k
The normal case without safety is:
s.push(max(k, s.top());
which pushes k onto the stack if it is bigger than what is already on the top of the stack, otherwise it pushes the value that is already on the top of the stack again.
The idea being that we only care about maximum values and we can discard anything smaller than the current maximum value
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 →
This is for safety to ensure we have something on the stack: s.size()>0?s.top():k
The normal case without safety is: s.push(max(k, s.top());
which pushes k onto the stack if it is bigger than what is already on the top of the stack, otherwise it pushes the value that is already on the top of the stack again.
The idea being that we only care about maximum values and we can discard anything smaller than the current maximum value