-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTCI-Stack-getMinimum.cpp
More file actions
46 lines (40 loc) · 954 Bytes
/
CTCI-Stack-getMinimum.cpp
File metadata and controls
46 lines (40 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
class MinOperationStack{
stack<int> integerStack;
stack<int> minStack;
public:
void pushInto(int value){
if(integerStack.empty()){
integerStack.push(value);
minStack.push(value);
}else{
if(value<=minStack.top())
minStack.push(value);
integerStack.push(value);
}
}
int popOut(){
if(integerStack.empty()) return -1;
if(minStack.top() == integerStack.top()){
minStack.pop();
}
int topElement = integerStack.top();
integerStack.pop();
return topElement;
}
int getMinimum(){
return minStack.top();
}
};
int main() {
MinOperationStack st;
cout<<st.popOut()<<endl;
st.pushInto(3);
st.pushInto(4);
cout<<st.getMinimum()<<endl;
st.pushInto(2);
//st.popOut();
cout<<st.getMinimum()<<endl;
return 0;
}