当前位置:网站首页>力扣题解 单调栈
力扣题解 单调栈
2022-07-27 05:20:00 【RL-UAV】
739. 每日温度
通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置,此时我们就要想到可以用单调栈了。
- 情况一:当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况
- 情况二:当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况
- 情况三:当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况
错误 : (!st.empty() && T[i] > T[st.top()]) 栈非空
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
// 递增栈
stack<int> st;
vector<int> result(T.size(), 0);
st.push(0);
for (int i = 1; i < T.size(); i++) {
if (T[i] < T[st.top()]) {
// 情况一
st.push(i);
} else if (T[i] == T[st.top()]) {
// 情况二
st.push(i);
} else {
while (!st.empty() && T[i] > T[st.top()]) {
// 情况三
result[st.top()] = i - st.top();
st.pop();
}
st.push(i);
}
}
return result;
}
};
精简:
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
stack<int> st; // 递增栈
vector<int> result(T.size(), 0);
for (int i = 0; i < T.size(); i++) {
while (!st.empty() && T[i] > T[st.top()]) {
// 注意栈不能为空
result[st.top()] = i - st.top();
st.pop();
}
st.push(i);
}
return result;
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(n)
496.下一个更大元素 I
// 版本二
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int> st;
vector<int> result(nums1.size(), -1);
if (nums1.size() == 0) return result;
unordered_map<int, int> umap; // key:下标元素,value:下标
for (int i = 0; i < nums1.size(); i++) {
umap[nums1[i]] = i;
}
st.push(0);
for (int i = 1; i < nums2.size(); i++) {
while (!st.empty() && nums2[i] > nums2[st.top()]) {
if (umap.count(nums2[st.top()]) > 0) {
// 看map里是否存在这个元素
int index = umap[nums2[st.top()]]; // 根据map找到nums2[st.top()] 在 nums1中的下标
result[index] = nums2[i];
}
st.pop();
}
st.push(i);
}
return result;
}
};
503.下一个更大元素II
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
vector<int> result(nums.size(), -1);
if (nums.size() == 0) return result;
stack<int> st;
for (int i = 0; i < nums.size() * 2; i++) {
// 模拟遍历两边nums,注意一下都是用i % nums.size()来操作
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;
}
};
42. 接雨水
class Solution {
public:
int trap(vector<int>& height) {
stack<int> st;
st.push(0);
int sum = 0;
for (int i = 1; i < height.size(); i++) {
while (!st.empty() && height[i] > height[st.top()]) {
int mid = st.top();
st.pop();
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;
}
};
84.柱状图中最大的矩形
总结:
int w = i - st.top() - 1; 有两行
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> st;
heights.insert(heights.begin(), 0); // 数组头部加入元素0
heights.push_back(0); // 数组尾部加入元素0 直接插入不用写结尾。
st.push(0);
int result = 0;
for (int i = 1; i < heights.size(); i++) {
while (heights[i] < heights[st.top()]) {
int mid = st.top();
st.pop();
int w = i - st.top() - 1;
int h = heights[mid];
result = max(result, w * h);
}
st.push(i);
}
return result;
}
};
边栏推荐
猜你喜欢

能替代ps的修图软件?

A photo breaks through the face recognition system: you can nod your head and open your mouth, netizens

Kaggle调用自定义模块方法

Greedy high performance neural network and AI chip application research and training

13. Logistic regression

C语言扫雷最新 递归展开 超详解(附源码)

【5·20特辑】MatLAb之我在和你表白

【11】二进制编码:“手持两把锟斤拷,口中疾呼烫烫烫”?

李宏毅 2020 深度学习与人类语言处理 DLHLP-Coreference Resolution-p21

维度问题以及等高线
随机推荐
C语言-文件操作
关于pytorch转onnx经常出现的问题
【头歌】重生之CNN图片分类基础
谈谈为何需要将类的成员函数声明为private
【Arduino】重生之Arduino 学僧(1)
编程学习记录——递归解决汉诺塔问题
【头歌】重生之深度学习篇-Keras(初级)
韦东山 数码相框 项目学习(三)freetype的移植
韦东山 数码相框 项目学习(四)简易的TXT文档显示器(电纸书)
Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
LaTeX中多个公式公用一个序号时
[first song] rebirth of me in py introductory training (6): definition and application of functions
Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)
[first song] rebirth of me in py introductory training (2): formula programming
Greedy high performance neural network and AI chip application research and training
【头歌】重生之我在py入门实训中(7): 函数调用
Lightroom Classic 2022 v11.4中文版「最新资源」
Weidongshan digital photo frame project learning (III) transplantation of freetype
2022.6.10 stm32mp157 serial port clock learning
力扣题解 动态规划(3)