-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinandMaxstackcode.cpp
More file actions
226 lines (155 loc) · 7.33 KB
/
MinandMaxstackcode.cpp
File metadata and controls
226 lines (155 loc) · 7.33 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
class MinStack {
private:
stack<int> mainStack;
stack<int> minStack;
public:
MinStack() {
}
void push(int val) {
mainStack.push(val);
if (minStack.empty() || val <= minStack.top()) {
minStack.push(val);
}
}
void pop() {
if (mainStack.top() == minStack.top()) {
minStack.pop();
}
mainStack.pop();
}
int top() {
return mainStack.top();
}
int getMin() {
return minStack.top();
}
};
Here's the sequence of operations again:
Push 5
Push 3
Push 7
Push 2
Push 9
Pop
Top
Pop
GetMin
Pop
After step 1, the minStack contains [5], and the mainStack contains [5].
After step 2, the minStack contains [5, 3], and the mainStack contains [5, 3].
After step 3, the minStack contains [5, 3], and the mainStack contains [5, 3, 7].
After step 4, the minStack contains [5, 3, 2], and the mainStack contains [5, 3, 7, 2].
After step 5, the minStack contains [5, 3, 2], and the mainStack contains [5, 3, 7, 2, 9].
Now, let's analyze the sequence of operations that follow:
Pop: We pop 9 from the mainStack. The minStack remains [5, 3, 2], and the mainStack becomes [5, 3, 7, 2].
Top: The top of the mainStack is 2, and the minStack still holds [5, 3, 2].
Pop: We pop 2 from the mainStack. Since 2 was the minimum element in the current window, it is also removed from the minStack. Now, the minStack becomes [5, 3], and the mainStack becomes [5, 3, 7].
GetMin: The minimum element in the current state is 3, which is at the top of the minStack.
Pop: We pop 7 from the mainStack. The minStack remains [5, 3], and the mainStack becomes [5, 3].
After these operations, the minStack contains [5, 3], and the mainStack contains [5, 3]. The minimum element throughout this sequence of operations is 5, as indicated by the top of the minStack.
In this specific example, the minimum element is 5 because it was the first element pushed into the stack, and subsequent elements pushed were greater than 5. The minStack retains this minimum element, and any changes in the minimum value are tracked as elements are pushed and popped from the stack.
User
ok do same for maxstack
ChatGPT
Certainly! Let's walk through an example of how a Max Stack works using the same sequence of operations. The Max Stack supports the same operations as a regular stack, but it also allows efficient retrieval of the maximum element in constant time.
Here's the sequence of operations again:
Push 5
Push 3
Push 7
Push 2
Push 9
Pop
Top
Pop
GetMax
Pop
We'll use a similar approach with two stacks: mainStack to store all elements and maxStack to store the maximum elements.
After step 1, the maxStack contains [5], and the mainStack contains [5].
After step 2, the maxStack contains [5], and the mainStack contains [5, 3].
After step 3, the maxStack contains [7], and the mainStack contains [5, 3, 7].
After step 4, the maxStack contains [7], and the mainStack contains [5, 3, 7, 2].
After step 5, the maxStack contains [9], and the mainStack contains [5, 3, 7, 2, 9].
Now, let's analyze the sequence of operations that follow:
Pop: We pop 9 from the mainStack. The maxStack remains [9], and the mainStack becomes [5, 3, 7, 2].
Top: The top of the mainStack is 2, and the maxStack still holds [9].
Pop: We pop 2 from the mainStack. The maxStack remains [9], and the mainStack becomes [5, 3, 7].
GetMax: The maximum element in the current state is 9, which is at the top of the maxStack.
Pop: We pop 7 from the mainStack. The maxStack remains [9], and the mainStack becomes [5, 3].
After these operations, the maxStack still contains [9], and the mainStack contains [5, 3]. The maximum element throughout this sequence of operations is 9, as indicated by the top of the maxStack.
In this example, the maximum element is 9 because it was the largest element pushed into the stack, and subsequent elements pushed were smaller than 9. The maxStack retains this maximum element, and any changes in the maximum value are tracked as elements are pushed and popped from the stack.
User
code
ChatGPT
Certainly! Here's the implementation of a Max Stack using two stacks in C++:
cpp
Copy code
class MaxStack {
private:
stack<int> mainStack;
stack<int> maxStack;
public:
MaxStack() {
}
void push(int val) {
mainStack.push(val);
if (maxStack.empty() || val >= maxStack.top()) {
maxStack.push(val);
}
}
void pop() {
if (mainStack.top() == maxStack.top()) {
maxStack.pop();
}
mainStack.pop();
}
int top() {
return mainStack.top();
}
int getMax() {
return maxStack.top();
}
};
This implementation works similarly to the Min Stack implementation, but it maintains the maximum value in the maxStack as elements are pushed and popped. The getMax operation efficiently returns the maximum element in constant time by simply looking at the top of the maxStack.
You can use the MaxStack class to perform the same sequence of operations that we discussed earlier. Just like the Min Stack, this implementation ensures that the maximum element is always available in constant time.
User
what is different in this code from min
ChatGPT
The code for the Max Stack is quite similar to the code for the Min Stack, with a few adjustments to handle finding and tracking the maximum element. The main difference lies in how the maxStack is updated and used to track the maximum element.
Here's a summary of the differences between the two implementations:
Push Operation:
For the Min Stack, you check if the new element is less than or equal to the top of the minStack before pushing it onto the minStack.
For the Max Stack, you check if the new element is greater than or equal to the top of the maxStack before pushing it onto the maxStack.
Pop Operation:
For both stacks, you pop the top element from the mainStack.
Additionally, for the Max Stack, you check if the element being popped from the mainStack is the same as the element at the top of the maxStack. If it is, you also pop the top element from the maxStack.
Get Operation (Min/Max Retrieval):
For the Min Stack, you retrieve the minimum element from the top of the minStack.
For the Max Stack, you retrieve the maximum element from the top of the maxStack.
In both implementations, the mainStack is used to store all elements, and the auxiliary stack (minStack or maxStack) is used to keep track of either the minimum or maximum element encountered so far.
The core concept of maintaining two synchronized stacks to efficiently retrieve the minimum or maximum element remains the same in both implementations. The choice between using <= or >= comparisons and updating the respective auxiliary stack accordingly determines whether you are implementing a Min Stack or a Max Stack.
class MaxStack {
private:
stack<int> mainStack;
stack<int> maxStack;
public:
MaxStack() {
}
void push(int val) {
mainStack.push(val);
if (maxStack.empty() || val >= maxStack.top()) {
maxStack.push(val);
}
}
void pop() {
if (mainStack.top() == maxStack.top()) {
maxStack.pop();
}
mainStack.pop();
}
int top() {
return mainStack.top();
}
int getMax() {
return maxStack.top();
}
};