-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-043.cpp
55 lines (54 loc) · 1.31 KB
/
Day-043.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
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
class Stack{
private:
vector<int>temp_stack;
vector<int>max_stack;
public:
void push(int x){
temp_stack.push_back(x);
if(max_stack.size()==0){
max_stack.push_back(x);
}else{
int t = move(max_stack.back());
max_stack.push_back(std::max(t,x));
}
}
void pop(void){
try{
if(temp_stack.size()==0){
throw "Emtpy Stack";
}
temp_stack.pop_back();
max_stack.pop_back();
}catch(const char *s){
cout<<s<<'\n';
}
}
int max(void){
try{
if(max_stack.size()==0){
throw "Emtpy Stack";
}
return max_stack.back();
}catch(const char *s){
cout<<s<<'\n';
return -1;
}
}
};
int main(int argc , char *argv[]){
// write you code here
Stack s;
s.push(100);
s.push(44);
cout<<"max : "<<s.max()<<'\n';
s.push(300);
cout<<"max : "<<s.max()<<'\n';
s.pop();
s.pop();
cout<<"max : "<<s.max()<<'\n';
s.pop();
s.pop();
return 0;
}