Opening the reading…
Opening the reading…
STACK › MONOTONIC STACK
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 →At position i, water can remain only when bars on both sides are taller than height[i]. The left side contributes the tallest bar seen to the left, and the right side contributes the tallest bar seen to the right. Therefore the water above position i is min(leftMax, rightMax) - height[i], unless the current bar is itself the new maximum and leaves no water there.
The two-pointer method avoids storing both maximum arrays. If height[left] is smaller than height[right], the left side can be resolved now: the right boundary is at least height[right], while leftMax records the best boundary seen from the left. The right side might contain an even taller bar, but that cannot reduce the water available on the left below leftMax. The symmetric argument resolves the right side when height[right] is smaller.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | Each iteration advances exactly one pointer, and neither pointer ever moves backward. Together they make at most n - 1 inward moves, so no bar is examined more than once. |
| Space | O(1) extra | The algorithm stores only two indices, two running maxima, and the answer. The returned amount is required output and is excluded from extra space; even for a strictly increasing, strictly decreasing, or alternating input, the bound remains O(1). |
#include <vector>
using namespace std;
class Solution {
public:
int trap(vector<int>& height) {
int n = height.size();
if (n == 0) return 0;
int left = 0;
int right = n - 1;
int leftMax = 0;
int rightMax = 0;
int ans = 0;
while (left < right) {
if (height[left] < height[right]) {
if (height[left] >= leftMax) {
leftMax = height[left];
} else {
ans += leftMax - height[left];
}
left++;
} else {
if (height[right] >= rightMax) {
rightMax = height[right];
} else {
ans += rightMax - height[right];
}
right--;
}
}
return ans;
}
};The key placement is the comparison before either pointer moves. When the left height is smaller, the right height is already a valid boundary for the left side, so leftMax - height[left] is final. The same reasoning applies in reverse on the right. Updating a maximum instead of adding water handles a bar that becomes the new wall.
A monotonic decreasing stack stores indices of bars whose right boundary has not been found yet. When a new bar is taller than the stack top, it closes a basin: the popped bar is the basin floor, the new bar is the right wall, and the new stack top is the left wall. This approach is not an optimisation; it uses O(n) extra space to make basin boundaries explicit, while the two-pointer solution uses O(1).
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
int trap(vector<int>& height) {
stack<int> st;
int ans = 0;
for (int i = 0; i < static_cast<int>(height.size()); i++) {
while (!st.empty() && height[i] > height[st.top()]) {
int bottom = st.top();
st.pop();
if (st.empty()) break;
int left = st.top();
int width = i - left - 1;
int boundedHeight = min(height[left], height[i]) - height[bottom];
ans += width * boundedHeight;
}
st.push(i);
}
return ans;
}
};