Opening the reading…
Opening the reading…
STACK › DESIGN PROBLEMS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A normal stack gives you only one piece of information for free: its top value. When that value is removed, the next value becomes visible, but the minimum among all remaining values may be buried anywhere below it. Recomputing the minimum after every pop would scan the stack and break the constant-time requirement.
Store the answer to the minimum query alongside every stack state. The main stack stores the pushed values, while a second stack stores the minimum value after each push. Both stacks therefore have the same height. Popping removes one entry from each, so the second stack's top immediately becomes the minimum of the remaining main stack.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | Each operation performs a fixed number of stack pushes, pops, comparisons, or top reads. The loop visits each operation once, and no operation scans either stack. |
| Space | O(n) extra, worst case O(n) when pushes fill the stack | The two working stacks can each contain one entry per active pushed value, so n pushes create linear auxiliary storage. The returned result array is required output and is excluded from the extra-space bound; the worst shape is a sequence containing only pushes. |
#include <algorithm>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> solve(vector<string> ops, vector<vector<int>> args) {
vector<string> result;
stack<int> values;
stack<int> minimums;
for (int i = 0; i < static_cast<int>(ops.size()); ++i) {
const string& op = ops[i];
if (op == "MinStack") {
result.push_back("null");
} else if (op == "push") {
int value = args[i][0];
values.push(value);
if (minimums.empty()) {
minimums.push(value);
} else {
minimums.push(min(value, minimums.top()));
}
result.push_back("null");
} else if (op == "pop") {
values.pop();
minimums.pop();
result.push_back("null");
} else if (op == "top") {
result.push_back(to_string(values.top()));
} else if (op == "getMin") {
result.push_back(to_string(minimums.top()));
}
}
return result;
}
};The important placement is the minimum-stack push inside the push branch. It records the minimum after the new value has joined the stack, while the paired pop removes exactly that historical state. The value stack is still needed because a later top operation asks for the actual top, not the minimum.
You can store each value together with the minimum of the stack through that node. A vector of pairs replaces the two synchronized stacks: the pair's first field is the pushed value, and its second field is the minimum at that height. This is merely a different arrangement, not an asymptotic optimisation; it uses linear extra space and constant time per operation just like the two-stack version.
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> solve(vector<string> ops, vector<vector<int>> args) {
vector<string> result;
vector<pair<int, int>> stack;
for (int i = 0; i < static_cast<int>(ops.size()); ++i) {
const string& op = ops[i];
if (op == "MinStack") {
result.push_back("null");
} else if (op == "push") {
int value = args[i][0];
int currentMinimum = stack.empty()
? value
: min(value, stack.back().second);
stack.push_back({value, currentMinimum});
result.push_back("null");
} else if (op == "pop") {
stack.pop_back();
result.push_back("null");
} else if (op == "top") {
result.push_back(to_string(stack.back().first));
} else if (op == "getMin") {
result.push_back(to_string(stack.back().second));
}
}
return result;
}
};