forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMin Stack.cpp
More file actions
37 lines (32 loc) · 829 Bytes
/
Min Stack.cpp
File metadata and controls
37 lines (32 loc) · 829 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
// Runtime: 29 ms (Top 73.41%) | Memory: 16.5 MB (Top 10.27%)
class MinStack {
public:
stack<int> stk;
stack<int> minstk;
MinStack() {
//leave empty because we don't need to initialise an object while it being created
}
void push(int val) {
//---------------Push in original stack------------------------------
stk.push(val);
//-------Put always minimum element to MIN stack--------------
if(minstk.size()==0)
minstk.push(val);
else{
if(minstk.top()<val)
minstk.push(minstk.top());
else
minstk.push(val);
}
}
void pop() {
stk.pop();
minstk.pop();
}
int top() {
return stk.top();
}
int getMin() {
return minstk.top();
}
};