单调栈
目录
leetcode 739 每日温度
题意:给出一个数组,返回一个vector
temperatures = [73,74,75,71,69,72,76,73]
st = []
单调栈原理建议b站灵茶学习.
C++ 逆序遍历版本
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n, 0);
stack<int> st ;
for(int i = n-1;i>=0;i--){ // 倒序遍历
int temp = temperatures[i];
while(!st.empty() && temperatures[st.top()]<=temp){
st.pop();
}
if(!st.empty()){
res[i] = st.top()-i;
}
st.push(i);
}
return res;
}
};
C++ 正序遍历版本
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n, 0);
stack<int> st ;
for(int i =0;i<n;i++){
int temp = temperatures[i];
while(!st.empty() && temperatures[st.top()]<temp ){
res[st.top()] = i-st.top();
st.pop();
}
st.push(i);
}
return res;
}
};
leetcode 1475. 商品折扣后的最终价格
题意:给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。
商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且
prices[j] <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。
请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。
Eg.
输入:prices = [8,4,6,2,3]
输出:[4,2,4,2,3]
商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
商品 3 和 4 都没有折扣。
C++ 正序版本
class Solution {
public:
vector<int> finalPrices(vector<int>& prices) {
int n = prices.size();
stack<int> st;
vector<int> res(n);
for(int i =0;i<n;i++){ // st 单调递增
int p = prices[i];
while(!st.empty() && prices[st.top()]>= p){
res[st.top()] -= p;
st.pop();
}
st.push(i);
res[i] = prices[i];
}
return res;
}
};
C++ 逆序版本
class Solution {
public:
vector<int> finalPrices(vector<int>& prices) {
int n = prices.size();
stack<int> st;
vector<int> res(n);
for(int i=n-1;i>=0;i--){
int p = prices[i];
res[i] = p;
while(!st.empty() && prices[st.top()] > p){
st.pop();
}
if(!st.empty()){
res[i] = prices[i] - prices[st.top()];
}
st.push(i);
}
return res;
}
};
leetcode 496. 下一个更大元素 I
题意:nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。
给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。
对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。
返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。
输入:nums1 = [4,1,2], nums2 = [1,3,4,2].
输出:[-1,3,-1]
解释:nums1 中每个值的下一个更大元素如下所述:
- 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
- 1 ,用加粗斜体标识,nums2 = [1,3,4,2]。下一个更大元素是 3 。
- 2 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
C++ 逆序
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size(), m = nums2.size();
unordered_map<int,int> mp;
vector<int> res(n, -1);
// 逆序
stack<int> st;
for(int i =m-1;i>=0;i--){
int num = nums2[i];
while(!st.empty() && st.top() < num){
st.pop();
}
if(!st.empty()){
mp[num] = st.top();
}else {
mp[num] = -1;
}
st.push(num);
}
for(int i=0;i<n;i++) res[i] = mp[nums1[i]];
return res;
}
};
leetcode 503. 下一个更大元素 II
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> res(n);
stack<int>st;
for(int i = n*2-1;i>=0;i--){
int num = nums[i%n];
while(!st.empty() && st.top() <= num){
st.pop();
}
res[i%n] = st.empty()?-1: st.top();
st.push(nums[i%n]);
}
return res;
}
};
leetcode 1019. 链表中的下一个更大节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<int> nextLargerNodes(ListNode* head) {
stack<pair<int,int>> st; // 递减
vector<int> res;
ListNode* p = head;
int i = 0;
while(p!=nullptr){ // 正序
int num = p->val;
while(!st.empty() && st.top().second < num){
res[st.top().first] = num;
st.pop();
}
st.push(make_pair(i, num));
res.push_back(0); i++;
p =p->next;
}
return res;
}
};
leetcode 962. 最大宽度坡
class Solution {
public:
int maxWidthRamp(vector<int>& nums) {
int n = nums.size();
stack<int> st;
int res = 0;
for(int i =0;i<n;i++){ // 构建一个严格递减的单调栈
if(st.empty() || nums[st.top()] >nums[i] ){
st.push(i);
}
}
for(int i = n-1;i>=0;i--){
while(!st.empty() && nums[st.top()]<=nums[i] ){
res = max(res, i - st.top());
st.pop();
}
}
return res;
}
};

浙公网安备 33010602011771号