Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. class MinStack {
  6. stack<int> myStack;
  7. stack<int> minStack;
  8. public:
  9. /** initialize your data structure here. */
  10. MinStack() {
  11.  
  12. }
  13.  
  14. void push(int x) {
  15. myStack.push(x);
  16. if(minStack.empty() || minStack.top() >= x) {
  17. minStack.push(x);
  18. }
  19. }
  20.  
  21. void pop() {
  22. int curVal = myStack.top();
  23. if(curVal == minStack.top())
  24. minStack.pop();
  25.  
  26. myStack.pop();
  27. }
  28.  
  29. int top() {
  30. return myStack.top();
  31. }
  32.  
  33. int getMin() {
  34. return minStack.top();
  35. }
  36. };
  37.  
  38. int main()
  39. {
  40. MinStack myStack;
  41. myStack.push(1);
  42. myStack.push(2);
  43. myStack.push(-1);
  44. myStack.push(-1);
  45. cout << myStack.getMin() << endl;
  46. cout << myStack.top() << endl;
  47. myStack.push(3);
  48. myStack.pop();
  49. myStack.pop();
  50. cout << myStack.getMin() << endl;
  51. cout << myStack.top() << endl;
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement