We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 921a4a9 commit 96d871bCopy full SHA for 96d871b
src/3_Stack_Queue/min_in_stack.cpp
@@ -28,9 +28,10 @@
28
* of both the top element and the minimum element after a pop operation.
29
*/
30
31
+#include <algorithm>
32
+#include <cassert>
33
#include <iostream>
34
#include <optional>
-#include <cassert>
35
#include <stack>
36
#include <string>
37
@@ -54,13 +55,18 @@ template <typename T> class StackWithMin {
54
55
}
56
57
void pop() {
- assert(!data_.empty());
58
+ if (data_.empty()) {
59
+ return;
60
+ }
61
T top_val = data_.top();
62
if (top_val < *min_value_) {
63
// Restore the previous minimum.
64
min_value_ = 2 * *min_value_ - top_val;
65
66
data_.pop();
67
68
+ min_value_.reset();
69
70
71
72
std::optional<T> top() const {
0 commit comments