-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmin.cpp
104 lines (91 loc) · 1.69 KB
/
getmin.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//design a special stack that supports push(), pop() and getmin() operations
//getmin() returns minimum element from the stack
//all the operation should be O(1) time complexity and O(1) space complexity
#include <bits/stdc++.h>
using namespace std;
/*
How previous minimum element, prevMinEle is, 2*minEle - y in pop() when y is the popped element?
We pushed y as 2x - prevMinEle. Here prevMinEle is minEle before y was inserted
y = 2*x - prevMinEle
Value of minEle was made equal to x
minEle = x
new minEle = 2 * minEle - y
= 2*x - (2*x - prevMinEle)
= prevMinEle // This is what we wanted
*/
class SpecialStack{
stack<int> s;
int minEle;
public:
bool isempty(){ return s.empty(); }
void push(int x){
if(s.empty()){
s.push(x);
minEle = x;
return;
}
if(x >= minEle)
{
s.push(x);
}
else{
s.push(2*x-minEle);
minEle = x;
}
}
void pop(){
if(s.empty()){
cout << "Empty stack\n";
return;
}
int x = s.top();
s.pop();
if(x < minEle){
cout << "Popped: " << minEle << endl;
minEle = 2*minEle - x;
}
else{
cout << "Popped: " << x << endl;
}
}
int Top(){
int t = s.top();
if(t < minEle){
return minEle;
}
return t;
}
int getmin(){
if(s.empty()){
cout << "Empty stack\n";
return -1;
}
return minEle;
}
};
int main(){
int n,x;
SpecialStack s;
do{
cout << "1. Push 2. Pop 3. Getmin 4. Exit\n";
cout << "Enter choice: ";
cin >> n;
switch(n){
case 1:{
cout << "Enter element: ";
cin >> x;
s.push(x);
break;
}
case 2:{
s.pop();
break;
}
case 3:{
cout << "Minimum element: " << s.getmin() << endl;
break;
}
}
}while(n != 4);
return 0;
}