-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 5 GFG Get Max from Stack.cpp
More file actions
140 lines (116 loc) · 3.04 KB
/
Day 5 GFG Get Max from Stack.cpp
File metadata and controls
140 lines (116 loc) · 3.04 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Get Max from Stack
Given q queries, You task is to implement the following three functions for a stack:
push(x) – Insert an integer x onto the stack.
pop() – Remove the top element from the stack.
peek() - Return the top element from the stack. If the stack is empty, return -1.
getMax() – Retrieve the maximum element from the stack in O(1) time. If the stack is empty, return -1.
Each query can be one of the following:
1 x : Push x onto the stack.
2 : Pop the top element from the stack.
3: Return the top element from the stack. If the stack is empty, return -1.
4: Return the maximum element from the stack.
*/
//Solution 1 : using 2 stack(vector)
class Solution {
public:
vector<int>st;
vector<int>maxst={-1};
Solution() {
}
// Add an element to the top of Stack
void push(int x) {
// code here
if(x>=maxst.back()) maxst.push_back(x);
st.push_back(x);
}
// Remove the top element from the Stack
void pop() {
// code here
if(st.empty())return;
if(st.back()==maxst.back())maxst.pop_back();
st.pop_back();
}
// Returns top element of the Stack
int peek() {
// code here
if(st.size()==0)return -1;
return st.back();
}
// Finds minimum element of Stack
int getMax() {
// code here
return maxst.back();
}
};
//Solution 2 : by Using single stack
// C++ program to implement a stack that find
// maximum in stack in O(1) time and space.
#include <iostream>
#include <stack>
using namespace std;
// Class to implement a stack with getMax()
class MaxStack {
private:
stack<int> s;
int maxEle;
public:
MaxStack() {
maxEle = -1;
}
// Add an element to the top of Stack
void push(int x) {
if (s.empty()) {
maxEle = x;
s.push(x);
}
// If new number is greater than maxEle
else if (x > maxEle) {
s.push(2 * x - maxEle);
maxEle = x;
} else {
s.push(x);
}
}
// Remove the top element from the Stack
void pop() {
if (s.empty()) {
return;
}
int top = s.top();
s.pop();
// Maximum will change if the maximum element
// of the stack is being removed.
if (top > maxEle) {
maxEle = 2 * maxEle - top;
}
}
// Returns top element of the Stack
int peek() {
if (s.empty()) {
return -1;
}
int top = s.top();
// If maxEle < top means maxEle stores value of top.
return (maxEle < top) ? maxEle : top;
}
// Finds maximum element of Stack
int getMax() {
if (s.empty())
return -1;
// variable maxEle stores the maximum element
// in the stack
return maxEle;
}
};
int main() {
MaxStack stk;
stk.push(2);
stk.push(3);
cout << stk.peek() << " ";
stk.pop();
cout << stk.getMax() << " ";
stk.push(1);
cout << stk.getMax() << " ";
return 0;
}