Advertisement
SalmaYasser

Untitled

Dec 5th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. class MinStack {
  2. public:
  3. /** initialize your data structure here. */
  4.  
  5. stack <int> min_num , nums;
  6. MinStack() {
  7. stack <int> temp ;
  8. min_num = temp;
  9. nums = temp;
  10. }
  11.  
  12. void push(int x) {
  13. nums.push(x);
  14. if (min_num.empty())
  15. {
  16. min_num.push(x);
  17. }
  18. else
  19. {
  20. min_num.push(min (x,min_num.top()));
  21. }
  22.  
  23. }
  24.  
  25. void pop() {
  26. min_num.pop();
  27. nums.pop();
  28.  
  29. }
  30.  
  31. int top() {
  32. return nums.top();
  33. }
  34.  
  35. int getMin() {
  36. return min_num.top();
  37. }
  38. };
  39.  
  40. /**
  41. * Your MinStack object will be instantiated and called as such:
  42. * MinStack* obj = new MinStack();
  43. * obj->push(x);
  44. * obj->pop();
  45. * int param_3 = obj->top();
  46. * int param_4 = obj->getMin();
  47. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement