Easy solution using double stack


  • 0
    J

    Just use an ordinary stack s1 to response to the top(), and an additional stack s2 which will only push when x <= s2.top()
    Then we can get the min Element in constant time. Remember that when pop(), check if s1.top() == s2.top(),
    in which case we need to call s2.pop() too.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.