-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMin_Stack.cpp
44 lines (38 loc) · 1.05 KB
/
Min_Stack.cpp
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
/*
Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost.
Note
min operation will never be called if there is no number in the stack
Example
Operations: push(1), pop(), push(2), push(3), min(), push(1), min()
Return: 1, 2, 1
*/
#include <stack>
using namespace std;
class MinStack {
public:
stack<int> mainStack;
stack<int> secondStack;
MinStack() {
// do initialization if necessary
}
void push(int number) {
// write your code here
if (secondStack.empty() || number <= secondStack.top()) {
secondStack.push(number);
}
mainStack.push(number);
}
int pop() {
// write your code here
if (!secondStack.empty() && secondStack.top() == mainStack.top()) {
secondStack.pop();
}
int result = mainStack.top();
mainStack.pop();
return result;
}
int min() {
// write your code here
return secondStack.top();
}
};