-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignMinStack.cpp
More file actions
60 lines (48 loc) · 861 Bytes
/
DesignMinStack.cpp
File metadata and controls
60 lines (48 loc) · 861 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
class MinStack
{
public:
stack<pair<int, int>> s; // val , minVal
MinStack()
{
}
void push(int val)
{
if (s.empty())
{
s.push({val, val});
}
else
{
int minVal = min(val, s.top().second);
s.push({val, minVal});
}
}
void pop()
{
s.pop();
}
int top()
{
return s.top().first; // val
}
int getMin()
{
return s.top().second; // minVal
}
};
int main()
{
MinStack ms;
ms.push(-2);
ms.push(0);
ms.push(-3);
cout << "Minimum value : " << ms.getMin() << endl;
ms.pop();
cout << "Top value : " << ms.top() << endl;
cout << "Minimum value : " << ms.getMin() << endl;
return 0;
}