通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置,此时我们就要想到可以用单调栈了。
用一个栈来记录我们遍历过的元素
(一)每日温度
单调栈里只需要存放元素的下标i就可以了,如果需要使用对应的元素,直接T[i]就可以获取
单调栈记录遍历过的元素
当前遍历到的元素:T[i] 栈顶元素T[st.top()]
//如果T[i] > T[st.top()]
//注意这里要有一个while循环,一直出栈,直到T[i] <= T[st.top()]再把i入栈
//一直维持从栈顶到栈底是递增状态
result[st.top()] = i - st.top() // 这里更新的是栈顶记录的下表对应的result
st.pop()
st.push(i)
//如果T[i] <= T[st.top()]
st.push(i)
//最后栈中没有弹出的元素,默认都为0(初始化时的默认值)
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
stack<int> st;
vector<int> result(temperatures.size(), 0);
st.push(0);
for(int i = 1; i < temperatures.size(); i ++){
if(temperatures[i] <= temperatures[st.top()]){
st.push(i);
}else{
// 在这种情况下要避免st为空的情况
while(!st.empty() && temperatures[i] > temperatures[st.top()]){
result[st.top()] = i - st.top();
st.pop();
}
st.push(i);
}
}
return result;
}
};
(二)下一个更大元素I
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int> st;
vector<int> result(nums1.size(), -1);
// key:nums[i], value: index
unordered_map<int, int> umap;
for(int i = 0; i < nums1.size(); i ++){
umap[nums1[i]] = i;
}
st.push(0);
for(int i = 1; i < nums2.size(); i ++){
if(nums2[i] <= nums2[st.top()]){
st.push(i);
}else{
while(!st.empty() && nums2[i] > nums2[st.top()]){
// 判断当前栈顶元素是否是nums1中的元素
if(umap.find(nums2[st.top()]) != umap.end()){
int index = umap[nums2[st.top()]];
result[index] = nums2[i];
}
st.pop();
}
st.push(i);
}
}
return result;
}
};
(三)下一个更大元素II
循环队列,在for循环中模拟两个循环,对i取模
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
vector<int> result(nums.size(), -1);
stack<int> st;
st.push(0);
for(int i = 1; i < nums.size()*2; i ++){
if(nums[i % nums.size()] <= nums[st.top()]){
// 注意细节,压栈的都是取模后的值
st.push(i % nums.size());
}else{
while(!st.empty() && nums[i % nums.size()] > nums[st.top()]){
result[st.top()] = nums[i % nums.size()];
st.pop();
}
// 同上
st.push(i % nums.size());
}
}
return result;
}
};
(四)接雨水
主要就是三种情况
-
情况一:当前遍历的元素(柱子)高度小于栈顶元素的高度 height[i] < height[st.top()]
-
情况二:当前遍历的元素(柱子)高度等于栈顶元素的高度 height[i] ==height[st.top()]
-
情况三:当前遍历的元素(柱子)高度大于栈顶元素的高度 height[i] > height[st.top()]
栈顶元素依次作为沟槽底部,求左右两边第一个比他高的元素
对于情况三,第 i 个元素是右边第一个比他高的元素,单调栈的下一个元素为左边第一个比他高的元素
class Solution {
public:
int trap(vector<int>& height) {
int sum = 0;
stack<int> st;
st.push(0);
for(int i = 1; i < height.size(); i ++){
if(height[i] < height[st.top()]) st.push(i);
else if(height[i] == height[st.top()]){
st.pop();
st.push(i);
}else{
while(!st.empty() && height[i] > height[st.top()]){
int mid = st.top();
st.pop();
// 这里pop出去后要判断栈内是否还有元素,就是mid左边是否有比他高的元素
if(!st.empty()){
int h = min(height[st.top()], height[i]) - height[mid];
int w = i - st.top() - 1;
sum += h * w;
}
}
st.push(i);
}
}
return sum;
}
};
(五)柱状图中最大的矩形
与接雨水相反,求中间元素左右两边第一个比该元素小的元素
由栈顶到栈底递减
注意:为了避免最后一个元素和第一个元素因为没有right/left而得不到计算面积,在原数组的首位各加一个0
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int result = 0;
heights.insert(heights.begin(), 0); // 首位加0
heights.push_back(0); // 尾部加0
stack<int> st;
st.push(0);
for(int i = 1; i < heights.size(); i ++){
if(heights[i] > heights[st.top()]) st.push(i); // 情况一
else if(heights[i] == heights[st.top()]){// 情况二
st.pop();
st.push(i);
}else{// 情况三
while(!st.empty() && heights[i] < heights[st.top()]){
int mid = st.top();
st.pop();
if(!st.empty()){
int left = st.top();
int w = i - left - 1;
// 这里mid为最高的,以此为高度
int h = heights[mid];
result = max(result, w * h);
}
}
st.push(i);
}
}
return result;
}
};